100% found this document useful (2 votes)
727 views55 pages

The Algorithm Design Manual 3rd Edition Steven S. Skiena - Get The Ebook Instantly With Just One Click

The document promotes the ebook collection available at textbookfull.com, featuring various titles including 'The Algorithm Design Manual' by Steven S. Skiena. It highlights the importance of algorithm design for programmers and provides a brief overview of the third edition's updates, including new topics and improved pedagogical tools. Additionally, it mentions the availability of online resources and lecture materials for instructors.

Uploaded by

tifennpagah33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
727 views55 pages

The Algorithm Design Manual 3rd Edition Steven S. Skiena - Get The Ebook Instantly With Just One Click

The document promotes the ebook collection available at textbookfull.com, featuring various titles including 'The Algorithm Design Manual' by Steven S. Skiena. It highlights the importance of algorithm design for programmers and provides a brief overview of the third edition's updates, including new topics and improved pedagogical tools. Additionally, it mentions the availability of online resources and lecture materials for instructors.

Uploaded by

tifennpagah33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Explore the full ebook collection and download it now at textbookfull.

com

The Algorithm Design Manual 3rd Edition Steven S.


Skiena

https://textbookfull.com/product/the-algorithm-design-
manual-3rd-edition-steven-s-skiena/

OR CLICK HERE

DOWLOAD EBOOK

Browse and Get More Ebook Downloads Instantly at https://textbookfull.com


Click here to visit textbookfull.com and download textbook now
Your digital treasures (PDF, ePub, MOBI) await
Download instantly and pick your perfect format...

Read anywhere, anytime, on any device!

The Data Science Design Manual 1st Edition Steven S.


Skiena

https://textbookfull.com/product/the-data-science-design-manual-1st-
edition-steven-s-skiena/

textbookfull.com

Algorithm Design with Haskell Richard S. Bird

https://textbookfull.com/product/algorithm-design-with-haskell-
richard-s-bird/

textbookfull.com

The Ethical Algorithm The Science of Socially Aware


Algorithm Design 6th Edition Kearns

https://textbookfull.com/product/the-ethical-algorithm-the-science-of-
socially-aware-algorithm-design-6th-edition-kearns/

textbookfull.com

Design Drawing 3rd Edition Francis D K Ching Steven P


Juroszek

https://textbookfull.com/product/design-drawing-3rd-edition-francis-d-
k-ching-steven-p-juroszek/

textbookfull.com
Preclinical Manual of Prosthodontics, 3e 3rd Edition
Lakshmi S

https://textbookfull.com/product/preclinical-manual-of-
prosthodontics-3e-3rd-edition-lakshmi-s/

textbookfull.com

The Effective Security Officer s Training Manual 3rd


Edition Ralph Brislin (Auth.)

https://textbookfull.com/product/the-effective-security-officer-s-
training-manual-3rd-edition-ralph-brislin-auth/

textbookfull.com

The Psychology Student Writer s Manual and Reader s Guide


3rd Edition Jill M. Scott

https://textbookfull.com/product/the-psychology-student-writer-s-
manual-and-reader-s-guide-3rd-edition-jill-m-scott/

textbookfull.com

Food Analysis Laboratory Manual 3rd Edition S. Suzanne


Nielsen (Auth.)

https://textbookfull.com/product/food-analysis-laboratory-manual-3rd-
edition-s-suzanne-nielsen-auth/

textbookfull.com

Living for the Elderly A Design Manual A Design Manual


Second and Revised Edition Eckhard Feddersen

https://textbookfull.com/product/living-for-the-elderly-a-design-
manual-a-design-manual-second-and-revised-edition-eckhard-feddersen/

textbookfull.com
TEXTS IN COMPUTER SCIENCE
Texts in Computer Science

Series Editors
David Gries, Department of Computer Science, Cornell University, Ithaca, NY, USA
Orit Hazzan , Faculty of Education in Technology and Science, Technion—Israel Institute of
Technology, Haifa, Israel
Titles in this series now included in the Thomson Reuters Book Citation Index!
‘Texts in Computer Science’ (TCS) delivers high-quality instructional content for
undergraduates and graduates in all areas of computing and information science, with a
strong emphasis on core foundational and theoretical material but inclusive of some
prominent applications-related content. TCS books should be reasonably self-contained
and aim to provide students with modern and clear accounts of topics ranging across the
computing curriculum. As a result, the books are ideal for semester courses or for
individual self-study in cases where people need to expand their knowledge. All texts are
authored by established experts in their fields, reviewed internally and by the series editors,
and provide numerous examples, problems, and other pedagogical tools; many contain
fully worked solutions.
The TCS series is comprised of high-quality, self-contained books that have broad and
comprehensive coverage and are generally in hardback format and sometimes contain
color. For undergraduate textbooks that are likely to be more brief and modular in their
approach, require only black and white, and are under 275 pages, Springer offers the
flexibly designed Undergraduate Topics in Computer Science series, to which we refer
potential authors.

More information about this series at http://www.springer.com/series/3191


Steven S. Skiena

The Algorithm Design Manual


Third Edition

123
Steven S. Skiena
Department of Computer Science
Stony Brook University
Stony Brook, NY, USA

ISSN 1868-0941 ISSN 1868-095X (electronic)


Texts in Computer Science
ISBN 978-3-030-54255-9 ISBN 978-3-030-54256-6 (eBook)
https://doi.org/10.1007/978-3-030-54256-6

1st edition: © Springer-Verlag New York 1998


2nd edition: © Springer-Verlag London Limited 2008, Corrected printing 2012
3rd edition: © The Editor(s) (if applicable) and The Author(s), under exclusive license to Springer Nature
Switzerland AG 2020
This work is subject to copyright. All rights are solely and exclusively licensed by the Publisher, whether the
whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations,
recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does
not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective
laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this book are
believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give
a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that
may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and
institutional affiliations.

This Springer imprint is published by the registered company Springer Nature Switzerland AG
The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland
Preface

Many professional programmers are not well prepared to tackle algorithm design
problems. This is a pity, because the techniques of algorithm design form one
of the core practical technologies of computer science.
This book is intended as a manual on algorithm design, providing access to
combinatorial algorithm technology for both students and computer profession-
als. It is divided into two parts: Techniques and Resources. The former is a
general introduction to the design and analysis of computer algorithms. The Re-
sources section is intended for browsing and reference, and comprises the catalog
of algorithmic resources, implementations, and an extensive bibliography.

To the Reader
I have been gratified by the warm reception previous editions of The Algorithm
Design Manual have received, with over 60,000 copies sold in various formats
since first being published by Springer-Verlag in 1997. Translations have ap-
peared in Chinese, Japanese, and Russian. It has been recognized as a unique
guide to using algorithmic techniques to solve problems that often arise in prac-
tice.
Much has changed in the world since the second edition of The Algorithm
Design Manual was published in 2008. The popularity of my book soared as
software companies increasingly emphasized algorithmic questions during em-
ployment interviews, and many successful job candidates have trusted The Al-
gorithm Design Manual to help them prepare for their interviews.
Although algorithm design is perhaps the most classical area of computer
science, it continues to advance and change. Randomized algorithms and data
structures have become increasingly important, particularly techniques based
on hashing. Recent breakthroughs have reduced the algorithmic complexity of
the best algorithms known for such fundamental problems as finding minimum
spanning trees, graph isomorphism, and network flows. Indeed, if we date the
origins of modern algorithm design and analysis to about 1970, then roughly
20% of modern algorithmic history has happened since the second edition of
The Algorithm Design Manual.
The time has come for a new edition of my book, incorporating changes
in the algorithmic and industrial world plus the feedback I have received from
hundreds of readers. My major goals for the third edition are:

v
vi PREFACE

• To introduce or expand coverage of important topics like hashing, ran-


domized algorithms, divide and conquer, approximation algorithms, and
quantum computing in the first part of the book (Practical Algorithm
Design).

• To update the reference material for all the catalog problems in the second
part of the book (The Hitchhiker’s Guide to Algorithms).

• To take advantage of advances in color printing to produce more informa-


tive and eye-catching illustrations.

Three aspects of The Algorithm Design Manual have been particularly beloved:
(1) the hitchhiker’s guide to algorithms, (2) the war stories, and (3) the elec-
tronic component of the book. These features have been preserved and strength-
ened in this third edition:

• The Hitchhiker’s Guide to Algorithms – Since finding out what is known


about an algorithmic problem can be a difficult task, I provide a catalog of
the seventy-five most important algorithmic problems arising in practice.
By browsing through this catalog, the student or practitioner can quickly
identify what their problem is called, what is known about it, and how
they should proceed to solve it.
I have updated every section in response to the latest research results and
applications. Particular attention has been paid to updating discussion
of available software implementations for each problem, reflecting sources
such as GitHub, which have emerged since the previous edition.

• War stories – To provide a better perspective on how algorithm problems


arise in the real world, I include a collection of “war stories”, tales from my
experience on real problems. The moral of these stories is that algorithm
design and analysis is not just theory, but an important tool to be pulled
out and used as needed.
The new edition of the book updates the best of the old war stories,
plus adds new tales on randomized algorithms, divide and conquer, and
dynamic programming.

• Online component – Full lecture notes and a problem solution Wiki is


available on my website www.algorist.com. My algorithm lecture videos
have been watched over 900,000 times on YouTube. This website has been
updated in parallel with the book.

Equally important is what is not done in this book. I do not stress the
mathematical analysis of algorithms, leaving most of the analysis as informal
arguments. You will not find a single theorem anywhere in this book. When
more details are needed, the reader should study the cited programs or refer-
ences. The goal of this manual is to get you going in the right direction as
quickly as possible.
PREFACE vii

To the Instructor
This book covers enough material for a standard Introduction to Algorithms
course. It is assumed that the reader has completed the equivalent of a second
programming course, typically titled Data Structures or Computer Science II.
A full set of lecture slides for teaching this course are available online at
www.algorist.com. Further, I make available online video lectures using these
slides to teach a full-semester algorithm course. Let me help teach your course,
through the magic of the Internet!
I have made several pedagogical improvements throughout the book, includ-
ing:

• New material – To reflect recent developments in algorithm design, I have


added new chapters on randomized algorithms, divide and conquer, and
approximation algorithms. I also delve deeper into topics such as hashing.
But I have been careful to heed the readers who begged me to keep the
book of modest length. I have (painfully) removed less important material
to keep total expansion by page count under 10% over the previous edition.
• Clearer exposition – Reading through my text ten years later, I was thrilled
to find many sections where my writing seemed ethereal, but other places
that were a muddled mess. Every page in this manuscript has been edited
or rewritten for greater clarity, correctness and flow.
• More interview resources – The Algorithm Design Manual remains very
popular for interview prep, but this is a fast-paced world. I include more
and fresher interview problems, plus coding challenges associated with
interview sites like LeetCode and Hackerrank. I also include a new section
with advice on how to best prepare for interviews.
• Stop and think – Each of my course lectures begins with a “Problem of
the Day,” where I illustrate my thought process as I solve a topic-specific
homework problem – false starts and all. This edition had more Stop and
Think sections, which perform a similar mission for the reader.
• More and better homework problems – The third edition of The Algorithm
Design Manual has more and better homework exercises than the previous
one. I have added over a hundred exciting new problems, pruned some
less interesting problems, and clarified exercises that proved confusing or
ambiguous.
• Updated code style – The second edition featured algorithm implementa-
tions in C, replacing or augmenting pseudocode descriptions. These have
generally been well received, although certain aspects of my programming
have been condemned by some as old fashioned. All programs have been
revised and updated, and are structurally highlighted in color.
• Color images – My companion book The Data Science Design Manual
was printed with color images, and I was excited by how much this made
viii PREFACE

concepts clearer. Every single image in the The Algorithm Design Manual
is now rendered in living color, and the process of review has improved
the contents of most figures in the text.

Acknowledgments
Updating a book dedication every ten years focuses attention on the effects
of time. Over the lifespan of this book, Renee became my wife and then the
mother of our two children, Bonnie and Abby, who are now no longer children.
My father has left this world, but Mom and my brothers Len and Rob remain
a vital presence in my life. I dedicate this book to my family, new and old, here
and departed.
I would like to thank several people for their concrete contributions to this
new edition. Michael Alvin, Omar Amin, Emily Barker, and Jack Zheng were
critical to building the website infrastructure and dealing with a variety of
manuscript preparation issues. Their roles were played by Ricky Bradley, An-
drew Gaun, Zhong Li, Betson Thomas, and Dario Vlah on previous editions.
The world’s most careful reader, Robert Piché of Tampere University, and Stony
Brook students Peter Duffy, Olesia Elfimova, and Robert Matsibekker read early
versions of this edition, and saved both you and me the trouble of dealing with
many errata. Thanks also to my Springer-Verlag editors, Wayne Wheeler and
Simon Rees.
Several exercises were originated by colleagues or inspired by other texts.
Reconstructing the original sources years later can be challenging, but credits
for each problem (to the best of my recollection) appear on the website.
Much of what I know about algorithms I learned along with my graduate stu-
dents. Several of them (Yaw-Ling Lin, Sundaram Gopalakrishnan, Ting Chen,
Francine Evans, Harald Rau, Ricky Bradley, and Dimitris Margaritis) are the
real heroes of the war stories related within. My Stony Brook friends and algo-
rithm colleagues Estie Arkin, Michael Bender, Jing Chen, Rezaul Chowdhury,
Jie Gao, Joe Mitchell, and Rob Patro have always been a pleasure to work with.

Caveat
It is traditional for the author to magnanimously accept the blame for whatever
deficiencies remain. I don’t. Any errors, deficiencies, or problems in this book
are somebody else’s fault, but I would appreciate knowing about them so as to
determine who is to blame.
Steven S. Skiena
Department of Computer Science
Stony Brook University
Stony Brook, NY 11794-2424
http://www.cs.stonybrook.edu/~skiena
August 2020
Contents

I Practical Algorithm Design 1


1 Introduction to Algorithm Design 3
1.1 Robot Tour Optimization . . . . . . . . . . . . . . . . . . . . . . 5
1.2 Selecting the Right Jobs . . . . . . . . . . . . . . . . . . . . . . . 8
1.3 Reasoning about Correctness . . . . . . . . . . . . . . . . . . . . 11
1.3.1 Problems and Properties . . . . . . . . . . . . . . . . . . . 11
1.3.2 Expressing Algorithms . . . . . . . . . . . . . . . . . . . . 12
1.3.3 Demonstrating Incorrectness . . . . . . . . . . . . . . . . 13
1.4 Induction and Recursion . . . . . . . . . . . . . . . . . . . . . . . 15
1.5 Modeling the Problem . . . . . . . . . . . . . . . . . . . . . . . . 17
1.5.1 Combinatorial Objects . . . . . . . . . . . . . . . . . . . . 17
1.5.2 Recursive Objects . . . . . . . . . . . . . . . . . . . . . . 19
1.6 Proof by Contradiction . . . . . . . . . . . . . . . . . . . . . . . . 21
1.7 About the War Stories . . . . . . . . . . . . . . . . . . . . . . . . 22
1.8 War Story: Psychic Modeling . . . . . . . . . . . . . . . . . . . . 22
1.9 Estimation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

2 Algorithm Analysis 31
2.1 The RAM Model of Computation . . . . . . . . . . . . . . . . . . 31
2.1.1 Best-Case, Worst-Case, and Average-Case Complexity . . 32
2.2 The Big Oh Notation . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.3 Growth Rates and Dominance Relations . . . . . . . . . . . . . . 37
2.3.1 Dominance Relations . . . . . . . . . . . . . . . . . . . . . 38
2.4 Working with the Big Oh . . . . . . . . . . . . . . . . . . . . . . 39
2.4.1 Adding Functions . . . . . . . . . . . . . . . . . . . . . . . 40
2.4.2 Multiplying Functions . . . . . . . . . . . . . . . . . . . . 40
2.5 Reasoning about Efficiency . . . . . . . . . . . . . . . . . . . . . 41
2.5.1 Selection Sort . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.5.2 Insertion Sort . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.5.3 String Pattern Matching . . . . . . . . . . . . . . . . . . . 43
2.5.4 Matrix Multiplication . . . . . . . . . . . . . . . . . . . . 45
2.6 Summations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.7 Logarithms and Their Applications . . . . . . . . . . . . . . . . . 48

ix
x CONTENTS

2.7.1 Logarithms and Binary Search . . . . . . . . . . . . . . . 49


2.7.2 Logarithms and Trees . . . . . . . . . . . . . . . . . . . . 49
2.7.3 Logarithms and Bits . . . . . . . . . . . . . . . . . . . . . 50
2.7.4 Logarithms and Multiplication . . . . . . . . . . . . . . . 50
2.7.5 Fast Exponentiation . . . . . . . . . . . . . . . . . . . . . 50
2.7.6 Logarithms and Summations . . . . . . . . . . . . . . . . 51
2.7.7 Logarithms and Criminal Justice . . . . . . . . . . . . . . 51
2.8 Properties of Logarithms . . . . . . . . . . . . . . . . . . . . . . . 52
2.9 War Story: Mystery of the Pyramids . . . . . . . . . . . . . . . . 54
2.10 Advanced Analysis (*) . . . . . . . . . . . . . . . . . . . . . . . . 57
2.10.1 Esoteric Functions . . . . . . . . . . . . . . . . . . . . . . 57
2.10.2 Limits and Dominance Relations . . . . . . . . . . . . . . 58
2.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

3 Data Structures 69
3.1 Contiguous vs. Linked Data Structures . . . . . . . . . . . . . . . 69
3.1.1 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
3.1.2 Pointers and Linked Structures . . . . . . . . . . . . . . . 71
3.1.3 Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . 74
3.2 Containers: Stacks and Queues . . . . . . . . . . . . . . . . . . . 75
3.3 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
3.4 Binary Search Trees . . . . . . . . . . . . . . . . . . . . . . . . . 81
3.4.1 Implementing Binary Search Trees . . . . . . . . . . . . . 81
3.4.2 How Good are Binary Search Trees? . . . . . . . . . . . . 85
3.4.3 Balanced Search Trees . . . . . . . . . . . . . . . . . . . . 86
3.5 Priority Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
3.6 War Story: Stripping Triangulations . . . . . . . . . . . . . . . . 89
3.7 Hashing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
3.7.1 Collision Resolution . . . . . . . . . . . . . . . . . . . . . 93
3.7.2 Duplicate Detection via Hashing . . . . . . . . . . . . . . 95
3.7.3 Other Hashing Tricks . . . . . . . . . . . . . . . . . . . . 96
3.7.4 Canonicalization . . . . . . . . . . . . . . . . . . . . . . . 96
3.7.5 Compaction . . . . . . . . . . . . . . . . . . . . . . . . . . 97
3.8 Specialized Data Structures . . . . . . . . . . . . . . . . . . . . . 98
3.9 War Story: String ’em Up . . . . . . . . . . . . . . . . . . . . . . 98
3.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

4 Sorting 109
4.1 Applications of Sorting . . . . . . . . . . . . . . . . . . . . . . . . 109
4.2 Pragmatics of Sorting . . . . . . . . . . . . . . . . . . . . . . . . 113
4.3 Heapsort: Fast Sorting via Data Structures . . . . . . . . . . . . 115
4.3.1 Heaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
4.3.2 Constructing Heaps . . . . . . . . . . . . . . . . . . . . . 118
4.3.3 Extracting the Minimum . . . . . . . . . . . . . . . . . . 120
4.3.4 Faster Heap Construction (*) . . . . . . . . . . . . . . . . 122
4.3.5 Sorting by Incremental Insertion . . . . . . . . . . . . . . 124
CONTENTS xi

4.4 War Story: Give me a Ticket on an Airplane . . . . . . . . . . . 125


4.5 Mergesort: Sorting by Divide and Conquer . . . . . . . . . . . . 127
4.6 Quicksort: Sorting by Randomization . . . . . . . . . . . . . . . 130
4.6.1 Intuition: The Expected Case for Quicksort . . . . . . . . 132
4.6.2 Randomized Algorithms . . . . . . . . . . . . . . . . . . . 133
4.6.3 Is Quicksort Really Quick? . . . . . . . . . . . . . . . . . 135
4.7 Distribution Sort: Sorting via Bucketing . . . . . . . . . . . . . . 136
4.7.1 Lower Bounds for Sorting . . . . . . . . . . . . . . . . . . 137
4.8 War Story: Skiena for the Defense . . . . . . . . . . . . . . . . . 138
4.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

5 Divide and Conquer 147


5.1 Binary Search and Related Algorithms . . . . . . . . . . . . . . . 148
5.1.1 Counting Occurrences . . . . . . . . . . . . . . . . . . . . 148
5.1.2 One-Sided Binary Search . . . . . . . . . . . . . . . . . . 149
5.1.3 Square and Other Roots . . . . . . . . . . . . . . . . . . . 150
5.2 War Story: Finding the Bug in the Bug . . . . . . . . . . . . . . 150
5.3 Recurrence Relations . . . . . . . . . . . . . . . . . . . . . . . . . 152
5.3.1 Divide-and-Conquer Recurrences . . . . . . . . . . . . . . 153
5.4 Solving Divide-and-Conquer Recurrences . . . . . . . . . . . . . . 154
5.5 Fast Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . 155
5.6 Largest Subrange and Closest Pair . . . . . . . . . . . . . . . . . 157
5.7 Parallel Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . 159
5.7.1 Data Parallelism . . . . . . . . . . . . . . . . . . . . . . . 159
5.7.2 Pitfalls of Parallelism . . . . . . . . . . . . . . . . . . . . 160
5.8 War Story: Going Nowhere Fast . . . . . . . . . . . . . . . . . . 161
5.9 Convolution (*) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
5.9.1 Applications of Convolution . . . . . . . . . . . . . . . . . 163
5.9.2 Fast Polynomial Multiplication (**) . . . . . . . . . . . . 164
5.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166

6 Hashing and Randomized Algorithms 171


6.1 Probability Review . . . . . . . . . . . . . . . . . . . . . . . . . . 172
6.1.1 Probability . . . . . . . . . . . . . . . . . . . . . . . . . . 172
6.1.2 Compound Events and Independence . . . . . . . . . . . . 174
6.1.3 Conditional Probability . . . . . . . . . . . . . . . . . . . 175
6.1.4 Probability Distributions . . . . . . . . . . . . . . . . . . 176
6.1.5 Mean and Variance . . . . . . . . . . . . . . . . . . . . . . 176
6.1.6 Tossing Coins . . . . . . . . . . . . . . . . . . . . . . . . . 177
6.2 Understanding Balls and Bins . . . . . . . . . . . . . . . . . . . . 179
6.2.1 The Coupon Collector’s Problem . . . . . . . . . . . . . . 180
6.3 Why is Hashing a Randomized Algorithm? . . . . . . . . . . . . 181
6.4 Bloom Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
6.5 The Birthday Paradox and Perfect Hashing . . . . . . . . . . . . 184
6.6 Minwise Hashing . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
6.7 Efficient String Matching . . . . . . . . . . . . . . . . . . . . . . 188
xii CONTENTS

6.8 Primality Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . 190


6.9 War Story: Giving Knuth the Middle Initial . . . . . . . . . . . . 191
6.10 Where do Random Numbers Come From? . . . . . . . . . . . . . 192
6.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193

7 Graph Traversal 197


7.1 Flavors of Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
7.1.1 The Friendship Graph . . . . . . . . . . . . . . . . . . . . 201
7.2 Data Structures for Graphs . . . . . . . . . . . . . . . . . . . . . 203
7.3 War Story: I was a Victim of Moore’s Law . . . . . . . . . . . . 207
7.4 War Story: Getting the Graph . . . . . . . . . . . . . . . . . . . 210
7.5 Traversing a Graph . . . . . . . . . . . . . . . . . . . . . . . . . . 212
7.6 Breadth-First Search . . . . . . . . . . . . . . . . . . . . . . . . . 213
7.6.1 Exploiting Traversal . . . . . . . . . . . . . . . . . . . . . 216
7.6.2 Finding Paths . . . . . . . . . . . . . . . . . . . . . . . . . 217
7.7 Applications of Breadth-First Search . . . . . . . . . . . . . . . . 217
7.7.1 Connected Components . . . . . . . . . . . . . . . . . . . 218
7.7.2 Two-Coloring Graphs . . . . . . . . . . . . . . . . . . . . 219
7.8 Depth-First Search . . . . . . . . . . . . . . . . . . . . . . . . . . 221
7.9 Applications of Depth-First Search . . . . . . . . . . . . . . . . . 224
7.9.1 Finding Cycles . . . . . . . . . . . . . . . . . . . . . . . . 224
7.9.2 Articulation Vertices . . . . . . . . . . . . . . . . . . . . . 225
7.10 Depth-First Search on Directed Graphs . . . . . . . . . . . . . . 230
7.10.1 Topological Sorting . . . . . . . . . . . . . . . . . . . . . . 231
7.10.2 Strongly Connected Components . . . . . . . . . . . . . . 232
7.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235

8 Weighted Graph Algorithms 243


8.1 Minimum Spanning Trees . . . . . . . . . . . . . . . . . . . . . . 244
8.1.1 Prim’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . 245
8.1.2 Kruskal’s Algorithm . . . . . . . . . . . . . . . . . . . . . 248
8.1.3 The Union–Find Data Structure . . . . . . . . . . . . . . 250
8.1.4 Variations on Minimum Spanning Trees . . . . . . . . . . 253
8.2 War Story: Nothing but Nets . . . . . . . . . . . . . . . . . . . . 254
8.3 Shortest Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
8.3.1 Dijkstra’s Algorithm . . . . . . . . . . . . . . . . . . . . . 258
8.3.2 All-Pairs Shortest Path . . . . . . . . . . . . . . . . . . . 261
8.3.3 Transitive Closure . . . . . . . . . . . . . . . . . . . . . . 263
8.4 War Story: Dialing for Documents . . . . . . . . . . . . . . . . . 264
8.5 Network Flows and Bipartite Matching . . . . . . . . . . . . . . . 267
8.5.1 Bipartite Matching . . . . . . . . . . . . . . . . . . . . . . 267
8.5.2 Computing Network Flows . . . . . . . . . . . . . . . . . 268
8.6 Randomized Min-Cut . . . . . . . . . . . . . . . . . . . . . . . . 272
8.7 Design Graphs, Not Algorithms . . . . . . . . . . . . . . . . . . . 274
8.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
CONTENTS xiii

9 Combinatorial Search 281


9.1 Backtracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
9.2 Examples of Backtracking . . . . . . . . . . . . . . . . . . . . . . 284
9.2.1 Constructing All Subsets . . . . . . . . . . . . . . . . . . 284
9.2.2 Constructing All Permutations . . . . . . . . . . . . . . . 286
9.2.3 Constructing All Paths in a Graph . . . . . . . . . . . . . 287
9.3 Search Pruning . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
9.4 Sudoku . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
9.5 War Story: Covering Chessboards . . . . . . . . . . . . . . . . . 295
9.6 Best-First Search . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
9.7 The A* Heuristic . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
9.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303

10 Dynamic Programming 307


10.1 Caching vs. Computation . . . . . . . . . . . . . . . . . . . . . . 308
10.1.1 Fibonacci Numbers by Recursion . . . . . . . . . . . . . . 308
10.1.2 Fibonacci Numbers by Caching . . . . . . . . . . . . . . . 309
10.1.3 Fibonacci Numbers by Dynamic Programming . . . . . . 311
10.1.4 Binomial Coefficients . . . . . . . . . . . . . . . . . . . . . 312
10.2 Approximate String Matching . . . . . . . . . . . . . . . . . . . . 314
10.2.1 Edit Distance by Recursion . . . . . . . . . . . . . . . . . 315
10.2.2 Edit Distance by Dynamic Programming . . . . . . . . . 317
10.2.3 Reconstructing the Path . . . . . . . . . . . . . . . . . . . 318
10.2.4 Varieties of Edit Distance . . . . . . . . . . . . . . . . . . 321
10.3 Longest Increasing Subsequence . . . . . . . . . . . . . . . . . . . 324
10.4 War Story: Text Compression for Bar Codes . . . . . . . . . . . 326
10.5 Unordered Partition or Subset Sum . . . . . . . . . . . . . . . . . 329
10.6 War Story: The Balance of Power . . . . . . . . . . . . . . . . . 331
10.7 The Ordered Partition Problem . . . . . . . . . . . . . . . . . . . 333
10.8 Parsing Context-Free Grammars . . . . . . . . . . . . . . . . . . 337
10.9 Limitations of Dynamic Programming: TSP . . . . . . . . . . . . 339
10.9.1 When is Dynamic Programming Correct? . . . . . . . . . 340
10.9.2 When is Dynamic Programming Efficient? . . . . . . . . . 341
10.10War Story: What’s Past is Prolog . . . . . . . . . . . . . . . . . . 342
10.11Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345

11 NP-Completeness 355
11.1 Problems and Reductions . . . . . . . . . . . . . . . . . . . . . . 355
11.1.1 The Key Idea . . . . . . . . . . . . . . . . . . . . . . . . . 356
11.1.2 Decision Problems . . . . . . . . . . . . . . . . . . . . . . 357
11.2 Reductions for Algorithms . . . . . . . . . . . . . . . . . . . . . . 358
11.2.1 Closest Pair . . . . . . . . . . . . . . . . . . . . . . . . . . 358
11.2.2 Longest Increasing Subsequence . . . . . . . . . . . . . . . 359
11.2.3 Least Common Multiple . . . . . . . . . . . . . . . . . . . 359
11.2.4 Convex Hull (*) . . . . . . . . . . . . . . . . . . . . . . . 360
11.3 Elementary Hardness Reductions . . . . . . . . . . . . . . . . . . 361
xiv CONTENTS

11.3.1 Hamiltonian Cycle . . . . . . . . . . . . . . . . . . . . . . 362


11.3.2 Independent Set and Vertex Cover . . . . . . . . . . . . . 363
11.3.3 Clique . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
11.4 Satisfiability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
11.4.1 3-Satisfiability . . . . . . . . . . . . . . . . . . . . . . . . 367
11.5 Creative Reductions from SAT . . . . . . . . . . . . . . . . . . . 369
11.5.1 Vertex Cover . . . . . . . . . . . . . . . . . . . . . . . . . 369
11.5.2 Integer Programming . . . . . . . . . . . . . . . . . . . . 371
11.6 The Art of Proving Hardness . . . . . . . . . . . . . . . . . . . . 373
11.7 War Story: Hard Against the Clock . . . . . . . . . . . . . . . . 375
11.8 War Story: And Then I Failed . . . . . . . . . . . . . . . . . . . 377
11.9 P vs. NP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
11.9.1 Verification vs. Discovery . . . . . . . . . . . . . . . . . . 380
11.9.2 The Classes P and NP . . . . . . . . . . . . . . . . . . . . 380
11.9.3 Why Satisfiability is Hard . . . . . . . . . . . . . . . . . . 381
11.9.4 NP-hard vs. NP-complete? . . . . . . . . . . . . . . . . . 382
11.10Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383

12 Dealing with Hard Problems 389


12.1 Approximation Algorithms . . . . . . . . . . . . . . . . . . . . . 389
12.2 Approximating Vertex Cover . . . . . . . . . . . . . . . . . . . . 390
12.2.1 A Randomized Vertex Cover Heuristic . . . . . . . . . . . 392
12.3 Euclidean TSP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
12.3.1 The Christofides Heuristic . . . . . . . . . . . . . . . . . . 394
12.4 When Average is Good Enough . . . . . . . . . . . . . . . . . . . 396
12.4.1 Maximum k-SAT . . . . . . . . . . . . . . . . . . . . . . . 396
12.4.2 Maximum Acyclic Subgraph . . . . . . . . . . . . . . . . . 397
12.5 Set Cover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
12.6 Heuristic Search Methods . . . . . . . . . . . . . . . . . . . . . . 399
12.6.1 Random Sampling . . . . . . . . . . . . . . . . . . . . . . 400
12.6.2 Local Search . . . . . . . . . . . . . . . . . . . . . . . . . 402
12.6.3 Simulated Annealing . . . . . . . . . . . . . . . . . . . . . 406
12.6.4 Applications of Simulated Annealing . . . . . . . . . . . . 410
12.7 War Story: Only it is Not a Radio . . . . . . . . . . . . . . . . . 411
12.8 War Story: Annealing Arrays . . . . . . . . . . . . . . . . . . . . 414
12.9 Genetic Algorithms and Other Heuristics . . . . . . . . . . . . . 417
12.10Quantum Computing . . . . . . . . . . . . . . . . . . . . . . . . . 418
12.10.1 Properties of “Quantum” Computers . . . . . . . . . . . . 419
12.10.2 Grover’s Algorithm for Database Search . . . . . . . . . . 420
12.10.3 The Faster “Fourier Transform” . . . . . . . . . . . . . . 422
12.10.4 Shor’s Algorithm for Integer Factorization . . . . . . . . . 422
12.10.5 Prospects for Quantum Computing . . . . . . . . . . . . . 424
12.11Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426

13 How to Design Algorithms 429


13.1 Preparing for Tech Company Interviews . . . . . . . . . . . . . . 433
CONTENTS xv

II The Hitchhiker’s Guide to Algorithms 435


14 A Catalog of Algorithmic Problems 437

15 Data Structures 439


15.1 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
15.2 Priority Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
15.3 Suffix Trees and Arrays . . . . . . . . . . . . . . . . . . . . . . . 448
15.4 Graph Data Structures . . . . . . . . . . . . . . . . . . . . . . . . 452
15.5 Set Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . 456
15.6 Kd-Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460

16 Numerical Problems 465


16.1 Solving Linear Equations . . . . . . . . . . . . . . . . . . . . . . 467
16.2 Bandwidth Reduction . . . . . . . . . . . . . . . . . . . . . . . . 470
16.3 Matrix Multiplication . . . . . . . . . . . . . . . . . . . . . . . . 472
16.4 Determinants and Permanents . . . . . . . . . . . . . . . . . . . 475
16.5 Constrained/Unconstrained Optimization . . . . . . . . . . . . . 478
16.6 Linear Programming . . . . . . . . . . . . . . . . . . . . . . . . . 482
16.7 Random Number Generation . . . . . . . . . . . . . . . . . . . . 486
16.8 Factoring and Primality Testing . . . . . . . . . . . . . . . . . . . 490
16.9 Arbitrary-Precision Arithmetic . . . . . . . . . . . . . . . . . . . 493
16.10Knapsack Problem . . . . . . . . . . . . . . . . . . . . . . . . . . 497
16.11Discrete Fourier Transform . . . . . . . . . . . . . . . . . . . . . 501

17 Combinatorial Problems 505


17.1 Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
17.2 Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
17.3 Median and Selection . . . . . . . . . . . . . . . . . . . . . . . . . 514
17.4 Generating Permutations . . . . . . . . . . . . . . . . . . . . . . 517
17.5 Generating Subsets . . . . . . . . . . . . . . . . . . . . . . . . . . 521
17.6 Generating Partitions . . . . . . . . . . . . . . . . . . . . . . . . 524
17.7 Generating Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . 528
17.8 Calendrical Calculations . . . . . . . . . . . . . . . . . . . . . . . 532
17.9 Job Scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
17.10Satisfiability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537

18 Graph Problems: Polynomial Time 541


18.1 Connected Components . . . . . . . . . . . . . . . . . . . . . . . 542
18.2 Topological Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . 546
18.3 Minimum Spanning Tree . . . . . . . . . . . . . . . . . . . . . . . 549
18.4 Shortest Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
18.5 Transitive Closure and Reduction . . . . . . . . . . . . . . . . . . 559
18.6 Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
18.7 Eulerian Cycle/Chinese Postman . . . . . . . . . . . . . . . . . . 565
18.8 Edge and Vertex Connectivity . . . . . . . . . . . . . . . . . . . . 568
xvi CONTENTS

18.9 Network Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571


18.10Drawing Graphs Nicely . . . . . . . . . . . . . . . . . . . . . . . 574
18.11Drawing Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
18.12Planarity Detection and Embedding . . . . . . . . . . . . . . . . 581

19 Graph Problems: NP-Hard 585


19.1 Clique . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
19.2 Independent Set . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
19.3 Vertex Cover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
19.4 Traveling Salesman Problem . . . . . . . . . . . . . . . . . . . . . 594
19.5 Hamiltonian Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . 598
19.6 Graph Partition . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
19.7 Vertex Coloring . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
19.8 Edge Coloring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608
19.9 Graph Isomorphism . . . . . . . . . . . . . . . . . . . . . . . . . 610
19.10Steiner Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
19.11Feedback Edge/Vertex Set . . . . . . . . . . . . . . . . . . . . . . 618

20 Computational Geometry 621


20.1 Robust Geometric Primitives . . . . . . . . . . . . . . . . . . . . 622
20.2 Convex Hull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626
20.3 Triangulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
20.4 Voronoi Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . 634
20.5 Nearest-Neighbor Search . . . . . . . . . . . . . . . . . . . . . . . 637
20.6 Range Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
20.7 Point Location . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644
20.8 Intersection Detection . . . . . . . . . . . . . . . . . . . . . . . . 648
20.9 Bin Packing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652
20.10Medial-Axis Transform . . . . . . . . . . . . . . . . . . . . . . . . 655
20.11Polygon Partitioning . . . . . . . . . . . . . . . . . . . . . . . . . 658
20.12Simplifying Polygons . . . . . . . . . . . . . . . . . . . . . . . . . 661
20.13Shape Similarity . . . . . . . . . . . . . . . . . . . . . . . . . . . 664
20.14Motion Planning . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
20.15Maintaining Line Arrangements . . . . . . . . . . . . . . . . . . . 671
20.16Minkowski Sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674

21 Set and String Problems 677


21.1 Set Cover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678
21.2 Set Packing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682
21.3 String Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . 685
21.4 Approximate String Matching . . . . . . . . . . . . . . . . . . . . 688
21.5 Text Compression . . . . . . . . . . . . . . . . . . . . . . . . . . 693
21.6 Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697
21.7 Finite State Machine Minimization . . . . . . . . . . . . . . . . . 702
21.8 Longest Common Substring/Subsequence . . . . . . . . . . . . . 706
21.9 Shortest Common Superstring . . . . . . . . . . . . . . . . . . . . 709
CONTENTS xvii

22 Algorithmic Resources 713


22.1 Algorithm Libraries . . . . . . . . . . . . . . . . . . . . . . . . . 713
22.1.1 LEDA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713
22.1.2 CGAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714
22.1.3 Boost Graph Library . . . . . . . . . . . . . . . . . . . . . 714
22.1.4 Netlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714
22.1.5 Collected Algorithms of the ACM . . . . . . . . . . . . . 715
22.1.6 GitHub and SourceForge . . . . . . . . . . . . . . . . . . . 715
22.1.7 The Stanford GraphBase . . . . . . . . . . . . . . . . . . 715
22.1.8 Combinatorica . . . . . . . . . . . . . . . . . . . . . . . . 716
22.1.9 Programs from Books . . . . . . . . . . . . . . . . . . . . 716
22.2 Data Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717
22.3 Online Bibliographic Resources . . . . . . . . . . . . . . . . . . . 718
22.4 Professional Consulting Services . . . . . . . . . . . . . . . . . . 718

23 Bibliography 719

Index 769
Part I

Practical Algorithm Design


Chapter 1

Introduction to Algorithm
Design

What is an algorithm? An algorithm is a procedure to accomplish a specific


task. An algorithm is the idea behind any reasonable computer program.
To be interesting, an algorithm must solve a general, well-specified problem.
An algorithmic problem is specified by describing the complete set of instances
it must work on and of its output after running on one of these instances. This
distinction, between a problem and an instance of a problem, is fundamental.
For example, the algorithmic problem known as sorting is defined as follows:

Problem: Sorting
Input: A sequence of n keys a1 , . . . , an .
Output: The permutation (reordering) of the input sequence such that a1 ≤
a2 ≤ · · · ≤ an−1 ≤ an .

An instance of sorting might be an array of names, like {Mike, Bob, Sally,


Jill, Jan}, or a list of numbers like {154, 245, 568, 324, 654, 324}. Determining
that you are dealing with a general problem instead of an instance is your first
step towards solving it.
An algorithm is a procedure that takes any of the possible input instances
and transforms it to the desired output. There are many different algorithms
that can solve the problem of sorting. For example, insertion sort is a method
that starts with a single element (thus trivially forming a sorted list) and then
incrementally inserts the remaining elements so that the list remains sorted.
An animation of the logical flow of this algorithm on a particular instance (the
letters in the word “INSERTIONSORT”) is given in Figure 1.1.
This algorithm, implemented in C, is described below:

© The Editor(s) (if applicable) and The Author(s), under exclusive license to 3
Springer Nature Switzerland AG 2020
S. S. Skiena, The Algorithm Design Manual, Texts in Computer Science,
https://doi.org/10.1007/978-3-030-54256-6_1
4 CHAPTER 1. INTRODUCTION TO ALGORITHM DESIGN

I N S E R T I O N S O R T
I N S E R T I O N S O R T
I NS E R T I O N S O R T
E I N S R T I O N S O R T
E I N R S T I O N S O R T
E I N R S T IO N S O R T
E I I N R STO N S O R T
E I I N O RST N S O R T
E I I N N OR S T S O R T
E I I N N OR S S T OR T
E I I N N OOR S S T RT
E I I N N OOR R S S T T
E I I N N OO R R S S T T

Figure 1.1: Animation of insertion sort in action (time flows downward).

void insertion_sort(item_type s[], int n) {


int i, j; /* counters */

for (i = 1; i < n; i++) {


j = i;
while ((j > 0) && (s[j] < s[j - 1])) {
swap(&s[j], &s[j - 1]);
j = j-1;
}
}
}

Note the generality of this algorithm. It works just as well on names as it


does on numbers. Or anything else, given the appropriate comparison operation
(<) to test which of two keys should appear first in sorted order. It can be
readily verified that this algorithm correctly orders every possible input instance
according to our definition of the sorting problem.
There are three desirable properties for a good algorithm. We seek algo-
rithms that are correct and efficient, while being easy to implement. These
goals may not be simultaneously achievable. In industrial settings, any pro-
gram that seems to give good enough answers without slowing the application
down is often acceptable, regardless of whether a better algorithm exists. The
issue of finding the best possible answer or achieving maximum efficiency usually
arises in industry only after serious performance or legal troubles.
This chapter will focus on algorithm correctness, with our discussion of ef-
ficiency concerns deferred to Chapter 2. It is seldom obvious whether a given
algorithm correctly solves a given problem. Correct algorithms usually come
with a proof of correctness, which is an explanation of why we know that the
algorithm must take every instance of the problem to the desired result. But be-
fore we go further, it is important to demonstrate why it’s obvious never suffices
as a proof of correctness, and is usually flat-out wrong.
1.1. ROBOT TOUR OPTIMIZATION 5

0 0

8 1

7
2

3
5 4

Figure 1.2: A good instance for the nearest-neighbor heuristic. The rainbow
coloring (red to violet) reflects the order of incorporation.

1.1 Robot Tour Optimization


Let’s consider a problem that arises often in manufacturing, transportation,
and testing applications. Suppose we are given a robot arm equipped with a
tool, say a soldering iron. When manufacturing circuit boards, all the chips and
other components must be fastened onto the substrate. More specifically, each
chip has a set of contact points (or wires) that need be soldered to the board.
To program the robot arm for this job, we must first construct an ordering of
the contact points so that the robot visits (and solders) the first contact point,
then the second point, third, and so forth until the job is done. The robot arm
then proceeds back to the first contact point to prepare for the next board, thus
turning the tool-path into a closed tour, or cycle.
Robots are expensive devices, so we want the tour that minimizes the time
it takes to assemble the circuit board. A reasonable assumption is that the
robot arm moves with fixed speed, so the time to travel between two points is
proportional to their distance. In short, we must solve the following algorithm
problem:

Problem: Robot Tour Optimization


Input: A set S of n points in the plane.
Output: What is the shortest cycle tour that visits each point in the set S?

You are given the job of programming the robot arm. Stop right now and
think up an algorithm to solve this problem. I’ll be happy to wait for you. . .

Several algorithms might come to mind to solve this problem. Perhaps the
most popular idea is the nearest-neighbor heuristic. Starting from some point
p0 , we walk first to its nearest neighbor p1 . From p1 , we walk to its nearest
unvisited neighbor, thus excluding only p0 as a candidate. We now repeat this
process until we run out of unvisited points, after which we return to p0 to close
off the tour. Written in pseudo-code, the nearest-neighbor heuristic looks like
6 CHAPTER 1. INTRODUCTION TO ALGORITHM DESIGN

−21 −5 −1 0 1 3 11

−21 −5 −1 0 1 3 11

Figure 1.3: A bad instance for the nearest-neighbor heuristic, with the optimal
solution. Colors are sequenced as ordered in the rainbow.

this:
NearestNeighbor(P )
Pick and visit an initial point p0 from P
p = p0
i=0
While there are still unvisited points
i=i+1
Select pi to be the closest unvisited point to pi−1
Visit pi
Return to p0 from pn−1
This algorithm has a lot to recommend it. It is simple to understand and
implement. It makes sense to visit nearby points before we visit faraway points
to reduce the total travel time. The algorithm works perfectly on the example
in Figure 1.2. The nearest-neighbor rule is reasonably efficient, for it looks at
each pair of points (pi , pj ) at most twice: once when adding pi to the tour, the
other when adding pj . Against all these positives there is only one problem.
This algorithm is completely wrong.
Wrong? How can it be wrong? The algorithm always finds a tour, but it
doesn’t necessarily find the shortest possible tour. It doesn’t necessarily even
come close. Consider the set of points in Figure 1.3, all of which lie along a line.
The numbers describe the distance that each point lies to the left or right of
the point labeled “0”. When we start from the point “0” and repeatedly walk
to the nearest unvisited neighbor, we might keep jumping left–right–left–right
over “0” as the algorithm offers no advice on how to break ties. A much better
(indeed optimal) tour for these points starts from the left-most point and visits
each point as we walk right before returning at the left-most point.
Try now to imagine your boss’s delight as she watches a demo of your robot
arm hopscotching left–right–left–right during the assembly of such a simple
board.
1.1. ROBOT TOUR OPTIMIZATION 7

“But wait,” you might be saying. “The problem was in starting at point
“0.” Instead, why don’t we start the nearest-neighbor rule using the left-most
point as the initial point p0 ? By doing this, we will find the optimal solution
on this instance.”
That is 100% true, at least until we rotate our example by 90 degrees. Now
all points are equally left-most. If the point “0” were moved just slightly to the
left, it would be picked as the starting point. Now the robot arm will hopscotch
up–down–up–down instead of left–right–left–right, but the travel time will be
just as bad as before. No matter what you do to pick the first point, the
nearest-neighbor rule is doomed to work incorrectly on certain point sets.
Maybe what we need is a different approach. Always walking to the closest
point is too restrictive, since that seems to trap us into making moves we didn’t
want. A different idea might repeatedly connect the closest pair of endpoints
whose connection will not create a problem, such as premature termination of
the cycle. Each vertex begins as its own single vertex chain. After merging
everything together, we will end up with a single chain containing all the points
in it. Connecting the final two endpoints gives us a cycle. At any step during
the execution of this closest-pair heuristic, we will have a set of single vertices
and the end of vertex-disjoint chains available to merge. In pseudocode:

ClosestPair(P )
Let n be the number of points in set P .
For i = 1 to n − 1 do
d=∞
For each pair of endpoints (s, t) from distinct vertex chains
if dist(s, t) ≤ d then sm = s, tm = t, and d = dist(s, t)
Connect (sm , tm ) by an edge
Connect the two endpoints by an edge

This closest-pair rule does the right thing in the example in Figure 1.3.
It starts by connecting “0” to its two immediate neighbors, the points 1 and
−1. Subsequently, the next closest pair will alternate left–right, growing the
central path by one link at a time. The closest-pair heuristic is somewhat more
complicated and less efficient than the previous one, but at least it gives the
right answer in this example.
But not on all examples. Consider what this algorithm does on the point set
in Figure 1.4(l). It consists of two rows of equally spaced points, with the rows
slightly closer together (distance 1 − ) than the neighboring points are spaced
within each row (distance 1 + ). Thus, the closest pairs of points stretch across
the gap, not around the boundary. After we pair off these points, the closest re-
maining pairs will connect these pairs alternately around the boundary. The to-
tal path length of the closest-pair tour is 3(1−)+2(1+)+ (1 − )2 + (2 + 2)2 .
Compared to the tour shown in Figure 1.4(r), we travel over 20% farther than
necessary when  → 0. Examples exist where the penalty is considerably worse
than this.
Thus, this second algorithm is also wrong. Which one of these algorithms
8 CHAPTER 1. INTRODUCTION TO ALGORITHM DESIGN

1+ε 1+ε

1−ε 1−ε 1−ε 1−ε

1+ε 1+ε
(l) (r)

Figure 1.4: A bad instance for the closest-pair heuristic, with the optimal solu-
tion.

performs better? You can’t tell just by looking at them. Clearly, both heuristics
can end up with very bad tours on innocent-looking input.
At this point, you might wonder what a correct algorithm for our problem
looks like. Well, we could try enumerating all possible orderings of the set of
points, and then select the one that minimizes the total length:

OptimalTSP(P )
d=∞
For each of the n! permutations Pi of point set P
If (cost(Pi ) ≤ d) then d = cost(Pi ) and Pmin = Pi
Return Pmin

Since all possible orderings are considered, we are guaranteed to end up


with the shortest possible tour. This algorithm is correct, since we pick the
best of all the possibilities. But it is also extremely slow. Even a powerful
computer couldn’t hope to enumerate all the 20! = 2,432,902,008,176,640,000
orderings of 20 points within a day. For real circuit boards, where n ≈ 1,000,
forget about it. All of the world’s computers working full time wouldn’t come
close to finishing the problem before the end of the universe, at which point it
presumably becomes moot.
The quest for an efficient algorithm to solve this problem, called the traveling
salesman problem (TSP), will take us through much of this book. If you need
to know how the story ends, check out the catalog entry for TSP in Section 19.4
(page 594).

Take-Home Lesson: There is a fundamental difference between algorithms,


procedures that always produce a correct result, and heuristics, which may
usually do a good job but provide no guarantee of correctness.

1.2 Selecting the Right Jobs


Now consider the following scheduling problem. Imagine you are a highly in
demand actor, who has been presented with offers to star in n different movie
projects under development. Each offer comes specified with the first and last
1.2. SELECTING THE RIGHT JOBS 9

Tarjan of the Jungle The Four Volume Problem


The President’s Algorist Steiner’s Tree Process Terminated
Halting State Programming Challenges
"Discrete" Mathematics Calculated Bets

Figure 1.5: An instance of the non-overlapping movie scheduling problem. The


four red titles define an optimal solution.

day of filming. Whenever you accept a job, you must commit to being available
throughout this entire period. Thus, you cannot accept two jobs whose intervals
overlap.
For an artist such as yourself, the criterion for job acceptance is clear: you
want to make as much money as possible. Because each film pays the same fee,
this implies you seek the largest possible set of jobs (intervals) such that no two
of them conflict with each other.
For example, consider the available projects in Figure 1.5. You can star in
at most four films, namely “Discrete” Mathematics, Programming Challenges,
Calculated Bets, and one of either Halting State or Steiner’s Tree.
You (or your agent) must solve the following algorithmic scheduling problem:
Problem: Movie Scheduling Problem
Input: A set I of n intervals on the line.
Output: What is the largest subset of mutually non-overlapping intervals that
can be selected from I?
Now you (the algorist) are given the job of developing a scheduling algorithm
for this task. Stop right now and try to find one. Again, I’ll be happy to wait. . .

There are several ideas that may come to mind. One is based on the notion
that it is best to work whenever work is available. This implies that you should
start with the job with the earliest start date – after all, there is no other job
you can work on then, at least during the beginning of this period:

EarliestJobFirst(I)
Accept the earliest starting job j from I that does not overlap any
previously accepted job, and repeat until no more such jobs remain.

This idea makes sense, at least until we realize that accepting the earliest
job might block us from taking many other jobs if that first job is long. Check
out Figure 1.6(l), where the epic War and Peace is both the first job available
and long enough to kill off all other prospects.
This bad example naturally suggests another idea. The problem with War
and Peace is that it is too long. Perhaps we should instead start by taking
the shortest job, and keep seeking the shortest available job at every turn.
Maximizing the number of jobs we do in a given period is clearly connected to
the notion of banging them out as quickly as possible. This yields the heuristic:
10 CHAPTER 1. INTRODUCTION TO ALGORITHM DESIGN

War and Peace

(l) (r)

Figure 1.6: Bad instances for the (l) earliest job first and (r) shortest job first
heuristics. The optimal solutions are in red.

ShortestJobFirst(I)
While (I = ∅) do
Accept the shortest possible job j from I.
Delete j, and any interval that intersects j, from I.

Again this idea makes sense, at least until we realize that accepting the
shortest job might block us from taking two other jobs, as shown in Figure
1.6(r). While the maximum potential loss here seems smaller than with the
previous heuristic, it can still limit us to half the optimal payoff.
At this point, an algorithm where we try all possibilities may start to look
good. As with the TSP problem, we can be certain exhaustive search is correct.
If we ignore the details of testing whether a set of intervals are in fact disjoint,
it looks something like this:

ExhaustiveScheduling(I)
j=0
Smax = ∅
For each of the 2n subsets Si of intervals I
If (Si is mutually non-overlapping) and (size(Si ) > j)
then j = size(Si ) and Smax = Si .
Return Smax

But how slow is it? The key limitation is enumerating the 2n subsets of
n things. The good news is that this is much better than enumerating all n!
orders of n things, as proposed for the robot tour optimization problem. There
are only about one million subsets when n = 20, which can be enumerated
within seconds on a decent computer. However, when fed n = 100 movies, we
get 2100 subsets, which is much much greater than the 20! that made our robot
cry “uncle” in the previous problem.
The difference between our scheduling and robotics problems is that there is
an algorithm that solves movie scheduling both correctly and efficiently. Think
about the first job to terminate—that is, the interval x whose right endpoint
is left-most among all intervals. This role is played by “Discrete” Mathematics
in Figure 1.5. Other jobs may well have started before x, but all of these must
at least partially overlap each other. Thus, we can select at most one from the
group. The first of these jobs to terminate is x, so any of the overlapping jobs
potentially block out other opportunities to the right of it. Clearly we can never
lose by picking x. This suggests the following correct, efficient algorithm:
1.3. REASONING ABOUT CORRECTNESS 11

OptimalScheduling(I)
While (I = ∅) do
Accept the job j from I with the earliest completion date.
Delete j, and any interval which intersects j, from I.

Ensuring the optimal answer over all possible inputs is a difficult but often
achievable goal. Seeking counterexamples that break pretender algorithms is an
important part of the algorithm design process. Efficient algorithms are often
lurking out there; this book will develop your skills to help you find them.

Take-Home Lesson: Reasonable-looking algorithms can easily be incorrect. Al-


gorithm correctness is a property that must be carefully demonstrated.

1.3 Reasoning about Correctness


Hopefully, the previous examples have opened your eyes to the subtleties of
algorithm correctness. We need tools to distinguish correct algorithms from
incorrect ones, the primary one of which is called a proof.
A proper mathematical proof consists of several parts. First, there is a
clear, precise statement of what you are trying to prove. Second, there is a set
of assumptions of things that are taken to be true, and hence can be used as
part of the proof. Third, there is a chain of reasoning that takes you from these
assumptions to the statement you are trying to prove. Finally, there is a little
square ( ) or QED at the bottom to denote that you have finished, representing
the Latin phrase for “thus it is demonstrated.”
This book is not going to emphasize formal proofs of correctness, because
they are very difficult to do right and quite misleading when you do them wrong.
A proof is indeed a demonstration. Proofs are useful only when they are honest,
crisp arguments that explain why an algorithm satisfies a non-trivial correctness
property. Correct algorithms require careful exposition, and efforts to show both
correctness and not incorrectness.

1.3.1 Problems and Properties


Before we start thinking about algorithms, we need a careful description of the
problem that needs to be solved. Problem specifications have two parts: (1) the
set of allowed input instances, and (2) the required properties of the algorithm’s
output. It is impossible to prove the correctness of an algorithm for a fuzzily-
stated problem. Put another way, ask the wrong question and you will get the
wrong answer.
Some problem specifications allow too broad a class of input instances. Sup-
pose we had allowed film projects in our movie scheduling problem to have gaps
in production (e.g. filming in September and November but a hiatus in Octo-
ber). Then the schedule associated with any particular film would consist of a
given set of intervals. Our star would be free to take on two interleaving but not
overlapping projects (such as the above-mentioned film nested with one filming
12 CHAPTER 1. INTRODUCTION TO ALGORITHM DESIGN

in August and October). The earliest completion algorithm would not work for
such a generalized scheduling problem. Indeed, no efficient algorithm exists for
this generalized problem, as we will see in Section 11.3.2.

Take-Home Lesson: An important and honorable technique in algorithm de-


sign is to narrow the set of allowable instances until there is a correct and
efficient algorithm. For example, we can restrict a graph problem from general
graphs down to trees, or a geometric problem from two dimensions down to
one.
There are two common traps when specifying the output requirements of a
problem. The first is asking an ill-defined question. Asking for the best route
between two places on a map is a silly question, unless you define what best
means. Do you mean the shortest route in total distance, or the fastest route,
or the one minimizing the number of turns? All of these are liable to be different
things.
The second trap involves creating compound goals. The three route-planning
criteria mentioned above are all well-defined goals that lead to correct, efficient
optimization algorithms. But you must pick a single criterion. A goal like Find
the shortest route from a to b that doesn’t use more than twice as many turns as
necessary is perfectly well defined, but complicated to reason about and solve.
I encourage you to check out the problem statements for each of the seventy-
five catalog problems in Part II of this book. Finding the right formulation for
your problem is an important part of solving it. And studying the definition of
all these classic algorithm problems will help you recognize when someone else
has thought about similar problems before you.

1.3.2 Expressing Algorithms


Reasoning about an algorithm is impossible without a careful description of the
sequence of steps that are to be performed. The three most common forms of
algorithmic notation are (1) English, (2) pseudocode, or (3) a real programming
language. Pseudocode is perhaps the most mysterious of the bunch, but it is
best defined as a programming language that never complains about syntax
errors.
All three methods are useful because there is a natural tradeoff between
greater ease of expression and precision. English is the most natural but least
precise programming language, while Java and C/C++ are precise but difficult
to write and understand. Pseudocode is generally useful because it represents
a happy medium.
The choice of which notation is best depends upon which method you are
most comfortable with. I usually prefer to describe the ideas of an algorithm in
English (with pictures!), moving to a more formal, programming-language-like
pseudocode or even real code to clarify sufficiently tricky details.
A common mistake my students make is to use pseudocode to dress up an
ill-defined idea so that it looks more formal. Clarity should be the goal. For
Another Random Document on
Scribd Without Any Related Topics
strong a hold upon me as I had upon him. I was satisfied of the
truth of what he had told me in regard to his own “coppers.” I had
inquired for myself, and I realized that he was making ten if not
fifteen thousand dollars by his operation.
I felt compelled to take the step he suggested. I owed my bank
three thousand dollars, and while Aunt Rachel was so feeble, I had
no hope of obtaining the amount from her. I must do something to
save myself from possible exposure. The brilliant example of
Cormorin loomed up before me. If he had made a large sum in
“coppers,” there was no reason why I should not do the same. It was
necessary that I should make the effort, and I gave him the five
thousand dollars he had just returned to me, to be invested in
Bustumups.
“It will be a safe operation, Glasswood,” continued Cormorin.
“Bustumups are sure to go up.”
I did not regard this last expression as one to be taken in the
metaphorical sense.
“You have looked into this matter, Cormorin, and of course you
understand it. As things now stand, you and I must hang together.”
“That’s so; count on me for anything you want.”
“Thank you. Now won’t you have a bottle of champagne with
me?”
“I am much obliged to you, Glasswood, but I can’t stop any
longer now. I must get your stock for you before four, or it will cost
you twenty-five to-morrow.”
“You are confident that this is a safe thing for me—are you not?”
“Oh, perfectly confident!” exclaimed he. “If you don’t believe in it,
don’t do it.”
“I rely upon your statements, and go in upon the assurance of
what you say.”
“Of course you must run your own risk. I can only advise you to
do what I would do myself.”
“That’s enough.”
He left me to procure the certificates of stock in the Bustumup
Company. I was to wait in the private room I had taken until his
return. I was alone, and when I began to think what I was doing, I
was appalled at the possibility of failure. I was in debt to the bank in
the sum of eight thousand dollars. If my investment should go wrong
I could not hope to make good the loss. I should be obliged to flee
from my wife and my home, and end my days in exile, if I should be
so fortunate as to escape without detection. A cold sweat stood on
my forehead as I thought of the possibility of discovery, of being
arrested even before I supposed any one suspected me, and of
being condemned to the State Prison for ten years or more.
I rang the bell, and ordered a bottle of champagne. I drank
several glasses of it, and the fumes went to my brain. I felt better.
My thoughts began to flow in another direction under the influence
of the sparkling fluid. Bustumups would advance every day. In a
week or two they would go up to a hundred dollars a share. If they
did this, I should make twenty thousand dollars, besides having my
capital returned to me. I should be able to pay off the bank, and
have seventeen thousand dollars left. My dream of future success
was colored with the pinkiest tint of the wine I drank.
I intended to be cautious. If, after my stock had gone up to fifty,
there were any signs of a reaction, I would sell, and still make ten
thousand dollars. Cormorin was sure the stock would be twenty-five
the next day. If it was, I should clear twelve hundred and fifty
dollars. But if it only went up to thirty-five in a week, it would enable
me to pay off what I owed the bank, and I should be content even
with that.
My new friend brought me the coveted shares, and helped me
finish the bottle of champagne before me. For some reason or other
he declined to punish a second one with me, and we separated. I
went home with my shares in my pocket. When the fumes of the
champagne passed off, I was uneasy again. I felt that I stood upon
the brink of a precipice. If Bustumups went down instead of going
up, I was ruined. There was no possible way for me to redeem
myself.
Though my uncle knew I was dealing in stocks—or rather took
my word for it—and was plunging into a sea of speculation, he did
not warn me against it. He had not a word of caution to utter, and
probably had no suspicion that I might be tempted to meddle with
the funds of the bank. If he had been as solicitous as he pretended
to be for my welfare, he would have warned me of the perils of my
course. For my own part, my uncle was a mystery to me.
Lilian with the black silk in prospect, was as happy as a queen. In
the evening Tom Flynn called. He was hardly seated before Mrs.
Oliphant and Bertha made us a call. “Dear ma” appeared to be cured
of her evil propensity, probably because another daughter, through
my indirect agency, was in a fair way of being disposed of. We had
sacred music, and a lively time generally. I was quite satisfied that
Tom would, at no distant day, make my wife’s sister his bride. This
prospect was quite enough to appease Mrs. Oliphant, and she really
looked quite amiable under the indications of this happy event.
Tom escorted Bertha and her mother home at ten o’clock, and
the next day the noble fellow told me with a blush, that he did not
leave the house on Tremont Street till the clock struck twelve. A
question or two from me brought out the fact that they were
engaged. I envied Tom—he was so happy. Why should he not be?
He owed the bank nothing. He had not soiled his soul by taking what
did not belong to him. He was a strictly moral and religious young
man. He would have gone without his dinner rather than stay away
from the evening prayer-meeting. I say I envied him. I did; and I
would have given all the world, had it been mine to give, for his
peace of mind.
I could not sleep that night when I went to bed. I got up and
drank nearly half a bottle of Smith’s old sherry, which stupefied my
brain, and gave me the needed rest from the goadings of conscience
and the terrors of the future. My fate depended upon the success of
the Bustumup Company. If that went down, I might be called at any
time to flee from my wife, and wander in fear and trembling as an
exile in some strange land. If I was in peril of exposure I could not
remain to face the blast of popular condemnation. My pride would
not permit me to live where any man could look down upon me with
either pity or contempt.
At twelve o’clock, when I run out for a lunch, I found that
Bustumups were quoted at twenty-five. This fact assured me, for
already I had practically paid off more than one-third of my debt.
The stock went a little higher before two o’clock, and my courage
was correspondingly increased. I was rather disturbed, however, at
the close of the bank, to see my uncle in close conversation with Mr.
Bristlebach. I fancied that I was the subject of their remarks,
especially as the president cast frequent glances at me. Captain
Halliard looked ugly.
I had shown him a portion of the certificates which Cormorin had
lent me. He was a shrewd business man, and though he had not
objected to the statement that I had saved half my salary, and
invested it in stocks, he might well have doubted the truth of it.
Perhaps he had been thinking over my affairs, and had come to the
conclusion that my assertions were doubtful. On two occasions he
had driven me up to the payment of money, and both times I had
met the demand.
Cormorin told me that he always ascertained when the directors
intended to make an examination. Captain Halliard meant mischief.
He intended, at least, to put me in condition to let Aunt Rachel
alone. I am confident he did not really believe that I had borrowed
any thing of the bank; but probably he wanted to satisfy himself that
I did not obtain my ready money from the drawer. As the
conversation continued I became alarmed. The President almost
invariably left the bank soon after two o’clock. To-day he remained.
As he had done once before since I occupied my position, he might
examine the condition of the cash department.
I meant to be on the sure side. I ran into the bank where
Cormorin was, and told him what I suspected. He promptly offered
to help me out, on the same terms that I had performed a similar
service for him.
“I want eight thousand,” I whispered. “I will return it to-morrow
morning.”
“Eight thousand!” exclaimed he. “Why, you are only five thousand
short.”
“Eight,” I replied, firmly.
“How’s that?”
“I was three thousand short when I made the little arrangement.”
“Thunder!” ejaculated he, impatiently. “Then you are the eagle
and I am the lamb.”
“We are both honest fellows, and mean to pay all we owe,” I
replied. “Do you suppose I would have accommodated you, the
other day, if I had not been in hot water myself? Of course if I go
down, you go with me.”
“But the security?” he asked.
“Two hundred and fifty shares of Bustumups.”
“They are worth only six thousand or so.”
“But will be worth more than eight in a few days; you shall have
your bills back to-morrow morning, without fail.”
I gave him my certificates and he handed me the money; but he
gnashed his teeth as he did so. If I fell, I should drag him down with
me.
“Is everything right in your drawer?” asked Heavyside, the
cashier, slyly, of me, when I returned.
“Certainly it is,” I replied. “Why do you ask?”
“Bristlebach is going to look over our accounts and cash this
afternoon.”
“All right,” I answered, carelessly.
I deposited the eight thousand in my drawer, balanced my cash,
and put the trunk into the safe. Paying no attention to any one, and
especially not to my uncle, I sauntered leisurely out of the bank.
CHAPTER XVIII.
BUSTUMUPS AT FIFTY.

B
Y the ruse in which Cormorin had instructed me, and for which he
had furnished the funds, I had provided against any exposure.
By this time I was fully satisfied that my uncle was working
against me; not that he intended to ruin me, but only to
maintain his own power and influence over me. There are men of this
stamp in the world, who will punish their best friends when they
refuse to be guided by them. Captain Halliard was as jealous of his
influence as he was of his money.
As my account with the bank was now square, I had no fear of the
investigation which was in progress. Mr. Heavyside, who had never
been suspected of even an irregularity, had been so kind as to inform
me of the proposed examination. I had in him a good friend, and a
mortgage on his future fidelity to me. I should defeat my uncle this
time, as I had before, but it was annoying to be subjected to his
espionage, though I could not afford to have a serious quarrel with
him.
I went home at about the usual hour. My Bustumups had done so
well that I was tolerably light-hearted. Lilian was as joyous as a
dream in June. Bertha had been with her all the forenoon, and I
heard much in praise of Tom Flynn. We dined, and then I proposed to
Lilian that we should ride out into the country. She was glad to go,
and we went. On my return home at six o’clock, Biddy handed me a
note from Mr. Bristlebach. I recognized his heavy hand-writing, and
my blood ceased to flow in its channels. I tore open the envelope. It
was simply a request to appear at the bank immediately.
What could it mean? My cash was all right. They could not have
discovered the truth. That was simply impossible. If there was any
trouble at the present time, Cormorin, and not myself, would be the
sufferer. If there had been a discovery of the whole truth, Mr.
Bristlebach was not the man to have sent a note to me; he would
have sent a constable. I decided to go at once to the bank, for I was
satisfied, from the manner in which the message had come, and by
the assurance that my cash was all right, that nothing very serious
could be charged upon me. I told Lilian I was going down town for an
hour, and she did not bother me with any troublesome questions.
On my arrival at the bank I found the president and my uncle in
the directors’ room. Both of them looked severe, but Captain Halliard
did not seem to be so much at his ease as usual. I knew him well
enough to be able to read his thoughts, and whatever mischief was
brewing he was at the bottom of it.
“Mr. Glasswood, of course you are aware that There is a deficiency
in your account?” said Mr. Bristlebach.
“No, sir, I am not aware of it,” I replied; and as I spoke the literal
truth, I answered with confidence.
“You are not?”
“No, sir.”
“Did you balance your cash to-day?”
“I did, sir; and at half-past two it was all right.”
“You put a bold face on the matter.”
“Certainly I do, sir. I am innocent of the charge, and I can afford
to speak the truth.”
“Nevertheless, your cash is short.”
“It was not short at half-past two to-day,” I replied, glancing at my
uncle.
He was uneasy, and did not confront me when I gazed at him.
“It is not a large deficiency,” added Mr. Bristlebach, “but large
enough to demand inquiry.”
“May I ask how much you found it short,” I inquired.
“Only three hundred dollars.”
“There may be some mistake—I hope there is,” suggested my
uncle.
“Who counted the cash?” I asked.
“We counted it together,” replied the president. “I wish to add that
I do not regard you as a defaulter or any thing of that sort. I sent for
you to enable you to explain the matter.”
“I have no further explanation to make. I left my cash all right to-
day,” I added, confidently.
“He is so sure, that I rather think some mistake has been made,”
added Captain Halliard.
“Probably there has been. Mr. Glasswood, I have had the utmost
confidence in you. When I suspected you before, a second
examination convinced me of your integrity. I have no doubt it will be
so this time.”
“I cannot undertake to keep my cash right, if other persons are
allowed to go to my drawer,” I continued, rather savagely.
“What!” exclaimed my uncle, springing to his feet.
“I said what I meant to say,” I replied.
The remark hit just where I intended it should. Mr. Bristlebach and
my uncle had been counting my cash. I had left it all right. If the
deficiency was insignificant, it was still enough to ruin me. I had
already made up my mind how my cash happened to be short. If the
president had made the examination himself there would have been
no deficiency. Of course I mean to say that Captain Halliard himself
had been the author of the mischief. In other words, he had either
taken three hundred dollars from my cash, or had falsely reported his
count.
Before I ventured to make this violent statement, I put my uncle
fairly on trial, and called up all the circumstances of our present
relations to testify against him. He was determined to maintain his
influence over me, and to prevent me from saying any thing to Aunt
Rachel about him. I had refused to give up my house at his bidding,
and prevented him from obliging his friend, Mr. Brentbone. I had
roundly reproached him for his conduct to me, and used language
which he could not tolerate in any one. I was satisfied that he had a
strong motive for desiring to obtain a hold upon me.
A strong motive, however, is not sufficient to explain so dastardly
an act as that in which I had dared to implicate my uncle. A man of
integrity, simply an honest man, would not be guilty of so vile a deed.
Was my uncle capable of such an act? He had procured my situation
for me by bringing up a charge against Tom Flynn which both he and
I knew was false—one which he himself had disproved as soon as his
purpose was accomplished. If he would do one mean thing, he would
not halt at another.
He had compelled me to pay the thousand dollars I owed Aunt
Rachel, out of sheer malice, and only to put me in a position where
he could control me. The mild speech of the president of the bank
assured me that I was not to be harshly dealt with; and my uncle
gently suggested that there might be a mistake.
“Be careful what you say, Mr. Glasswood,” said the president.
“Now I’m going out to get a cup of tea; when I come back we will
ascertain whether there is a mistake or not.”
Mr. Bristlebach left the room. My uncle looked embarrassed, thrust
his fingers into his vest pockets, and seemed to be feeling for
something. I was tempted to spring upon him, and throw out the
contents of those pockets, for I was satisfied that the deficiency in my
cash could be accounted for only in that way.
“Paley, you have been speculating in coppers,” said he.
“I have; but that is my business,” I replied, roughly.
“I propose to pay the bank the amount your cash is short, and to
hush the matter up where it is.”
“I don’t ask you to do any thing of the sort.”
“I am on your bond, and I must do it. No matter about that. I
expected, after you told me what you were doing in coppers, to find a
deficit of thousands. I was prepared to pay even that, for you are of
my own flesh and blood.”
“You are very affectionate!”
“I have succeeded in quieting Mr. Bristlebach.”
“I see you have.”
“You talk to me as though I had done you an injury instead of a
kindness,” added he, reproachfully.
“That is what you have done.”
“Your cash is three hundred short,” said he, putting his hands into
his vest pockets again.
Perhaps I was insane under the pressure of his implied charge; at
any rate, under the impulse of the moment, without consciously
determining to do it, I sprung upon him like a tiger; and having no
warning of my purpose myself, I gave him none. I thrust my hands
into his vest pockets, and drew from them whatever they contained. I
retreated into the farther corner of the room to examine my capture.
The deed was done so quick that Captain Halliard had no time to
resist, though he seized me by the shoulders. I was furious, and
shook him off like a child.
“What do you mean, you villain?” gasped he.
I paid no attention to him, but proceeded to examine my prize.
Among other things I found three bills, of one hundred dollars each.
“Do you mean to rob me, Paley?” demanded he; but, like Hamlet’s
ghost, he appeared to be “more in sorrow than in anger;” and more
in fear than in sorrow.
“Do you carry your money in your vest pockets, sir?” I demanded.
“Sometimes I do.”
“You took these bills from my trunk when you counted my cash.”
“Nonsense, Paley!”
“I can swear to one of them, at least,” I replied, holding up one of
the bills, on the face of which some clown had written a sentence
about depreciated currency, that had attracted my attention. “I left
this bill in my trunk in the vault at half-past two to-day; at half-past
six I find it in your pocket.”
“Do you think—”
“I know!” I interrupted, him, in the most savage manner. “If I can
find a policeman, I will put you on the track to the State Prison.”
“Don’t be absurd, Paley,” interposed my uncle; but I saw that
there was no heart in the remark. “There must have been a mistake
in the counting.”
“You stole this money from my trunk to get me into trouble.”
“Didn’t I tell the president that I would pay the deficit?” asked my
uncle. “Hush up! There comes Mr. Bristlebach! Not a word of this to
him.”
“You confess, then, that you took this money from my trunk?”
“By-and-by we will talk about it,” he replied, with much agitation.
I had proved my case. My uncle was a villain. He had taken three
hundred dollars from my cash—not enough to make me look like a
defaulter—for the purpose of maintaining his influence over me, and
to keep me from telling bad stories about him to Aunt Rachel. Guilty
as I was, I made myself believe that I was an innocent man, because
I was not guilty in the direction he accused me. Mr. Bristlebach
returned to the room.
“I am satisfied, from what Mr. Glasswood says, that there must
have been a mistake in our count,” said my uncle. “As I told you, I
was confident my nephew was honest, but I was fearful, when I
learned that he had been speculating in coppers. I thought, as I was
on his bond, we had better look into the matter. I am perfectly
satisfied now.”
This very consistent statement was assented to by the president,
but my cash was counted again, at the request of Captain Halliard. I
was in doubt whether to restore the three hundred I had wrested
from the conspirator, but I concluded that I could not afford to expose
him. We counted the cash, which was mostly in large bills, and of
course I was fully vindicated. The president was profuse in his
apologies, and my uncle was kind enough to take the burden of the
blunder on himself. He could even see where he had made the
mistake. I left the bank with him, and we walked up the street
together.
“That was an awkward mistake of mine,” said he.
“Very,” I replied, with a sneer.
“But I think I can explain it.”
“I don’t think you can.”
“You seem to have taken it into your head that I mean to injure
you.”
“I have.”
“You are mistaken. I am on your bond. Money is so plenty with
you, that I was afraid I might be called upon to pay the bond.
Bristlebach is so intimate with me that I could satisfy myself without
doing you any harm. That was all I intended.”
“And that’s the reason why you took three hundred dollars out of
my trunk, I suppose?”
“Mr. Bristlebach handed me that money himself. I wanted to pay
out that amount to-night, and I drew a check for it. I entirely forgot it
when we counted the cash, and that was the deficit. Here is the
check; as you put the money back, I took the check from your
drawer. That’s the whole story.”
“Why didn’t you explain it to Mr. Bristlebach, then?” I asked,
believing not a word he said.
“Because it was so stupid of me to forget that the check had been
paid out of your cash.”
“Very stupid, indeed!”
“I will tell him about it to-morrow,” added my uncle.
As I have said before, a man in my situation could not afford to
quarrel with one so powerful as Captain Halliard. I kept my own
counsel, not wholly certain that he would not yet be called upon to
pay the amount of his bond on my account. We parted in peace, and
I was abundantly pleased that I had been able to fight off the charge.
The next morning, when I went to the bank, I took the eight
thousand from the cash, which Cormorin had lent me, and returned it
to him. He was a happy man then. I doubt whether he slept a wink
the night before, for the idea of being responsible for my deficit, as
well as his own, could not have been very comforting to him.
I was all right at the bank, and my uncle treated me with
“distinguished consideration.” On several occasions he assured me he
should use his influence in my favor with Aunt Rachel. If I wished for
the money he had compelled me to pay—solely for my own good—he
would let me have it again. Indeed, if I was short at any time, he
would lend me a thousand dollars. I thought I might have occasion to
avail myself of his offer, and I was pleasant and pliable. I said nothing
more about the three hundred dollars.
For a week all was well with me. Ballyhacks went up to seventy-
five; but Bustumups were slower, and had only touched forty in the
same time. This figure satisfied me, inasmuch as it enabled me to pay
my debt at the bank. Yet I believed, with the utmost confidence, that
there was five or ten thousand more in the stock for me, and as long
as things were easy at the bank, I did not think of realizing.
Then I was sick for ten days, and was obliged to stay in the
house, but even while my brain was on fire with fever I went down
town one day. I dared not leave my deficit to be discovered by my
substitute. I compelled poor Cormorin to lend me the eight thousand
again, on the security of my Bustumups. They were worth nearly this
sum in the market by this time, and he did not object very
strenuously.
As soon as I was able to get out, I hastened back to the bank,
and took my place at the counter. Cormorin had sold his stock at
eighty. Bustumups were quoted at fifty, with a prospect of a further
advance. My friend had made thirteen thousand dollars. When I had
made him whole, he instantly resigned his place, fearful, I think, of
getting into trouble through my agency. He went to New York, to go
into business there. I did not care. My stocks at fifty paid my debt,
and left me forty-five hundred surplus. I was excited over the
prospect. I should be a rich man in a few weeks.
But everything did not turn out just as I anticipated.
CHAPTER XIX.
A CRASH IN COPPERS.

I
WAS worth forty-five hundred dollars while Bustumups were
quoted at fifty. Every day, while they hung at about this figure,
I debated with myself the policy of selling, paying my debt, and
investing my surplus in some other concern. Perhaps I should
have done so, if I had known of a company in which I could place
entire confidence. I missed Cormorin very much, for I needed his
advice; and I had come to regard him as an oracle in the matter of
coppers.
It looked like madness to sacrifice a stock which might go up to
eighty or a hundred, as the Ballyhack had, and though my debt
worried me, I could not make up my mind to let it go. If I could put
ten thousand dollars in my pocket, my fortune would be made, for
with this sum I could operate on a large scale. There was no danger
of another examination of my cash at present, and I was secure. But
Bustumups did not advance as rapidly as I wished. They hung at
about fifty. I was told that parties were investigating the condition of
the mine, and that as soon as they reported, the stock would go up
as rapidly as Ballyhack had done. I was willing to wait patiently for a
week or two, while the stock about held its own. Its trifling
fluctuations up and down troubled me, but the parties who worked it
convinced me that these were only accidental changes.
Though I saw my uncle every day, he did not allude to his own
villainy, and I was prudent enough to wait until I was out of the
woods before I did so. In the course of a couple of weeks, when I
had made my ten thousand dollars, I intended to resign my position,
and then I could afford to express my mind very freely to Captain
Halliard. With ten thousand dollars in my exchequer, I could go into
any business that suited me, and make money enough to support
me in a style becoming my abilities.
I still had strong hopes that the fortune of Aunt Rachel would be
mine. She was now apparently rapidly regaining her health, and I
determined to improve my chances as soon as I could. On the
following Saturday afternoon I took Lilian down to Springhaven with
me, and we both used our best efforts to win her regard. I took her
out to ride, I read to her, and the old lady seemed as fond of me as
when I was a boy. I was her only nephew, and it had been often
reported that I was to be her heir, though on what authority I did
not know. I invited her to spend a week or a month at my house in
Boston, and she promised to do so as soon as she was able.
A rumor that the parties who were investigating the condition of
the mine intended to make a favorable report sent Bustumups to
fifty-five, and I was very happy. I was worth nearly six thousand
dollars. At the end of another week the stock went up to sixty, and
the balance of worldly wealth in my favor was seven thousand
dollars. The game was becoming intensely exciting. Another week or
so would realize all my hopes. I should be free and safe.
While every thing was in this cheerful condition Aunt Rachel sent
for me, and I hastened to Springhaven, for I could not afford to
neglect her summons. She was ready to go home with me, and she
accompanied me to my house in Needham street. The old lady was
a little surprised to find that I lived in elegant style, as she was
pleased to express it; but then she regarded the salary I received,
which was double what her minister had, as princely in itself. Simple
as were her views of social economy, she did not accuse me of
extravagance. Lilian understood the matter perfectly, and was all
tenderness and devotion.
One morning, after she had been at our house three days, Aunt
Rachel asked me if I knew a certain Squire Townsend, a lawyer,
whom the old lady had been acquainted with in the early years of
her life. I had heard of him. He was an attorney of the old school,
and I hoped she intended to make her will while she was thus kindly
disposed towards me. She begged me to see the old gentleman, and
ask him to call upon her during the forenoon.
“Do you see much of Captain Halliard, Paley?” asked my aunt, as
I was going out.
“I see him nearly every day.”
“I wonder he has not been up to see me yet,” added the old lady.
I did not wonder. I had not taken the trouble to tell him that Aunt
Rachel was at my house.
“Do you wish to see him?” I asked.
“Not particularly. He has done considerable business for me.”
“I know it. He did some for you while you were sick.”
“Did he?”
“He made me pay the thousand dollars I borrowed of you.”
“What, Captain Halliard!” exclaimed the old lady.
“He did.”
“Why, I didn’t tell him to do that.”
“I know you didn’t, but he showed me a power of attorney from
you, and I couldn’t have helped myself if I had wished to do so; but
I paid it, and it’s of no consequence now.”
“I didn’t mean you should pay that money. I shouldn’t have cried
if you had never paid it. I’ll talk with Squire Townsend about it.
Couldn’t you take care of my property for me just as well as your
uncle?”
“Well, I suppose I could,” I replied, rather indifferently.
“I never liked your uncle very well. He is too sharp for me. I’ll see
what can be done.”
“I wouldn’t say anything about meddling with Captain Halliard, at
present,” I suggested, for I was somewhat afraid of him myself.
“I’ll see about it; but I didn’t mean he should trouble you about
that money. He’d no business to do it, and I shall tell him so when I
see him.”
I did not intend she should see him at present. I went to the
office of Squire Townsend, on my way down town, and left a
message for him to call upon my aunt. I was fully persuaded in my
own mind that she intended to make a will, and that she had come
up to Boston in order to have the instrument drawn up by her old
friend. Every thing looked rosy to me, for the old lady would
certainly leave me the larger portion, if not the whole, of her worldly
wealth.
When I went home in the afternoon I learned that Squire
Townsend had spent a couple of hours with Aunt Rachel, but Lilian
had not heard a word that passed between them. Then the squire
had called a carriage, and they had gone off together. I was not very
anxious to know where they had gone, though I concluded that it
was only to the office of her old friend for the purpose of having the
will properly signed and witnessed. Now, as always before, Aunt
Rachel kept her own counsel. She never told how much she was
worth, or what she intended to do with her property. She was true
to her antecedents, and during the remainder of her stay she never
mentioned the nature of her business with Squire Townsend, as she
invariably called him. She said a good deal about the worthy lawyer’s
history, and told stories about him at school. She was glad to meet
him once more before she left the world, but she did not hint that
she had special business with him.
The old lady staid her week out, and then said she must go
home. She did not think the city agreed with her. She did not sleep
as well nights as at Springhaven. Both Lilian and I pressed her to
remain longer, and promised to do every thing we could to make her
happy, but she was resolute, and I attended her home, a week to a
day from the time she arrived.
I never saw her again.
During the week that Aunt Rachel was with me, Bustumups
began to look a little shaky. From sixty the stock went down to fifty-
five in one day, but it immediately rallied, and those who managed it
assured me it was only because money was a little tight, and a
considerable portion of the stock had been forced upon the market.
I proposed to sell, as I had promised myself that I would on the first
appearance of a decline.
“Don’t do it,” said the operator. “Wait three days, and you can
take sixty, if not sixty-five, for your stock. If you crowd it upon the
market at once, you will drive it down, and cheat yourself out of
twelve hundred dollars.”
But it looks shaky,” I pleaded.
“The best stocks on the street go up and down by turns. Wait till
day after to-morrow, at least.”
I did wait, because I did not like to have twenty-five hundred
dollars taken out of my pocket at one swoop. Two days after, I was
in a fever of anxiety about my Bustumups. They had gone up and
down under the influence of various rumors, good and bad, and no
one could foresee the end. At noon Tom Flynn went out for his
lunch.
“The coppers are in a bad way,” said he, taking his place at the
counter on his return.
“What is the matter with them?” I inquired, with my heart in my
throat, for my very reputation rested upon the prosperity of the
coppers.
“Ballyhacks have dropped down from eighty to fifty,” added Tom.
“What?” I exclaimed.
“That’s what they say. Did you own any?”
“No, no; no Ballyhacks,” I replied, struggling to conceal my
emotion.
I had not told Tom I was speculating in coppers, and I think he
knew nothing about it, though he might have heard something of
the kind.
“Did you own any coppers?” he inquired, with a tone and look
that indicated the sympathy he felt for me.
“None of any consequence,” I replied.
I dared not talk with him about the matter lest I should expose
my emotion. With the stunning intelligence he had communicated to
me on my mind, it was simply impossible for me to discharge my
duties in the bank. I could hardly tell a hundred-dollar bill from a
thousand. I told the cashier that I was sick, and was fearful that I
should faint again if I did not get out in the air. He took my place,
and I staggered out into the street. There were people on the
sidewalk, but I could not see them. Every thing seemed to be
without form or shape. I was in a fearful agony of mind, and
dreaded lest I should drop senseless upon the pavement.
I went into a saloon and drank a glass of brandy. I sat down at
one of the little tables to gather up my shattered senses. Ruin stared
me in the face. If Ballyhacks had fallen from eighty to fifty, what
hope could there be for Bustumups? After all, the mischief might be
confined to this particular stock, and mine might be still on the top
of the wave. The brandy I had drank seemed to have no effect upon
me. I took another glass, and my courage began to rise a little. The
saloon was nearly filled with people, and there was a confused
jabber of tongues all around me. Men spoke to me, and called me by
name. I replied mechanically, but I could not have told a minute
later who had spoken to me.
“But they are a fraud,” said a gentleman, seating himself at the
table next to mine.
“Certainly they are,” replied the other. “The Ballyhack mine has
produced some copper; but they say there is not a particle of metal
on the Bustumup track—not an ounce! The managers of this affair
ought to be indicted and sent to the State Prison.”
“Merciful Heavens!” I ejaculated to myself, “I am ruined!”
“Ballyhack has gone down to forty within half an hour,” added
one of the gentlemen.
“I heard a man offer Bustumups just now for twenty, and people
laughed at him,” added the other. “I don’t believe they will bring
ten.”
“Probably not. There is not a dollar of value in them. The thing is
an unmitigated swindle.”
The whole of the savage truth was poured into my ears. A
moment later, I heard some one say that the managers of the
Bustumup Company had found it convenient to disappear. I was
almost a maniac. I cursed my folly because I had not sold my stock
when it began to look shaky. The villains who had comforted me and
made promises that I should sell at sixty were simply designing
knaves, who had fraudulently worked this stock up to sixty, while
there was not a penny of real value in it.
The first shock bore heavily upon me, but I soon recovered in
some measure from its effect. I went into the street, and inquired
for myself, in regard to the coppers. There were two or three
substantial companies which were actually producing metal and
paying handsome dividends. The other companies were swindles;
and Bustumup was the most egregious humbug of the whole. I tried
to get an offer for my stock, and found it would not bring a dollar a
share. Indeed, it could not be sold at any price. In a word, the five
thousand dollars I had borrowed from the bank was a total loss.
I will not attempt to describe the misery into which I was so
suddenly plunged. If I had sold my stock a week before, I might
have paid my debt and had five thousand dollars left. Now I was a
defaulter in the sum of eight thousand dollars. It was horrible to
think of. There was no possible way, that I could see, to escape the
consequences. What should I do?
I went back to the bank and told Mr. Heavyside that I was better.
I resumed my place at the counter, and did my work till the bank
closed, sustained by the brandy I had drank. I tried to devise some
plan by which I could conceal my deficit for a time. I could think of
nothing satisfactory. An examination of the affairs of the bank was
sure to betray me. I was tempted to commit suicide, as others have
done under the same pressure of guilt.
I thought of my wife, and my eyes filled with tears, as I pictured
the fall to which she would be subjected. It was ruin to her as well
as to me. What would she do, while I was thinking of her in my
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like