!! Frontmatter
!! Frontmatter
Jeff Erickson
th edition (pre-publication draft) — December ,
Ωth edition (pre-publication draft) — April ,
st paperback edition — June ,
� — ��
ISBN: -- - - (paperback)
Shall I tell you, my friend, how you will come to understand it?
Go and write a book upon it.
— Henry Home, Lord Kames (�6�6–��8�),
in a letter to Sir Gilbert Elliot
The individual is always mistaken. He designed many things, and drew in other
persons as coadjutors, quarrelled with some or all, blundered much, and
something is done; all are a little advanced, but the individual is always mistaken.
It turns out somewhat new and very unlike what he promised himself.
— Ralph Waldo Emerson, “Experience”, Essays, Second Series (�8��)
What I have outlined above is the content of a book the realization of whose basic
plan and the incorporation of whose details would perhaps be impossible; what I
have written is a second or third draft of a preliminary version of this book
— Michael Spivak, preface of the �rst edition of
Differential Geometry, Volume I (����)
Preface
Prerequisites
The algorithms classes I teach at Illinois have two significant prerequisites:
a course on discrete mathematics and a course on fundamental data structures.
Consequently, this textbook is probably not suitable for most students as a first
i
P������
The book briefly covers some of this prerequisite material when it arises in
context, but more as a reminder than a good introduction. For a more thorough
overview, I strongly recommend the following freely available references:
ii
Additional References
Additional References
Please do not restrict yourself to this or any other single reference. Authors and
readers bring their own perspectives to any intellectual material; no instructor
“clicks” with every student, or even with every very strong student. Finding the
author that most effectively gets their intuition into your head takes some effort,
but that effort pays off handsomely in the long run.
The following references have been particularly valuable sources of intuition,
examples, exercises, and inspiration; this is not meant to be a complete list.
• Alfred V. Aho, John E. Hopcroft, and Jeffrey D. Ullman. The Design and
Analysis of Computer Algorithms. Addison-Wesley, . (I used this textbook
as an undergraduate at Rice and again as a masters student at UC Irvine.)
• Boaz Barak. Introduction to Theoretical Computer Science. Textbook draft,
most recently revised June . (Not your grandfather’s theoretical CS
textbook, and so much the better for it; the fact that it’s free is a delightful
bonus.)
• Thomas Cormen, Charles Leiserson, Ron Rivest, and Cliff Stein. Introduction
to Algorithms, third edition. MIT Press/McGraw-Hill, . (I used the first
edition as a teaching assistant at Berkeley.)
• Sanjoy Dasgupta, Christos H. Papadimitriou, and Umesh V. Vazirani. Algo-
rithms. McGraw-Hill, . (Probably the closest in content to this book,
but considerably less verbose.)
• Jeff Edmonds. How to Think about Algorithms. Cambridge University Press,
.
• Michael R. Garey and David S. Johnson. Computers and Intractability:
A Guide to the Theory of NP-Completeness. W. H. Freeman, .
iii
P������
iv
Steal This Book!
∆
• Orange stars indicate that you are eating Lucky Charms that were manu-
factured before . Ew.
These exercises are designed as opportunities to practice, not as targets for their
own sake. The goal of each problem is not to solve that specific problem, but to
exercise a certain set of skills, or to practice solving a certain type of problem.
Partly for this reason, I don’t provide solutions to the exercises; the solutions are
not the point. In particular, there is no “instructor’s manual”; if you can’t solve a
problem yourself, you probably shouldn’t assign it to your students. That said,
you can probably find solutions to whatever homework problems I’ve assigned
this semester on the web page of whatever course I’m teaching. And nothing is
stopping you from writing an instructor’s manual!
v
P������
Finally, please make whatever you write freely, easily, and globally avail-
able on the open web—not hidden behind the gates of a learning management
system or some other type of paywall—so that students and instructors else-
where can benefit from your unique insights. In particular, if you develop useful
resources that directly complement this textbook, such as slides, videos, or
solution manuals, please let me know so that I can add links to your resources
from the book web site.
Acknowledgments
This textbook draws heavily on the contributions of countless algorithms students,
teachers, and researchers. In particular, I am immensely grateful to more than
three thousand Illinois students who have used my lecture notes as a primary
reference, offered useful (if sometimes painful) criticism, and suffered through
some truly awful early drafts. Thanks also to many colleagues and students
around the world who have used these notes in their own classes and have sent
helpful feedback and bug reports.
I am particularly grateful for the feedback and contributions (especially
exercises) from my amazing teaching assistants:
Aditya Ramani, Akash Gautam, Alex Steiger, Alina Ene, Amir Nayyeri,
Asha Seetharam, Ashish Vulimiri, Ben Moseley, Brad Sturt, Brian Ensink,
Chao Xu, Charlie Carlson, Chris Neihengen, Connor Clark, Dan Bullok,
Dan Cranston, Daniel Khashabi, David Morrison, Ekta Manaktala, Erin
Wolf Chambers, Gail Steitz, Gio Kao, Grant Czajkowski, Hsien-Chih Chang,
Igor Gammer, Jacob Laurel, John Lee, Johnathon Fischer, Junqing Deng,
Kent Quanrud, Kevin Milans, Kevin Small, Konstantinos Koiliaris, Kyle Fox,
Kyle Jao, Lan Chen, Mark Idleman, Michael Bond, Mitch Harris, Naveen
Arivazhagen, Nick Bachmair, Nick Hurlburt, Nirman Kumar, Nitish Korula,
Patrick Lin, Phillip Shih, Rachit Agarwal, Reza Zamani-Nasab, Rishi Talreja,
Rob McCann, Sahand Mozaffari, Shalan Naqvi, Shripad Thite, Spencer
Gordon, Srihita Vatsavaya, Subhro Roy, Tana Wattanawaroon, Umang
Mathur, Vipul Goyal, Yasu Furakawa, and Yipu Wang.
I’ve also been helped tremendously by many discussions with faculty col-
leagues at Illinois: Alexandra Kolla, Cinda Heeren, Edgar Ramos, Herbert
Edelsbrunner, Jason Zych, Kim Whittlesey, Lenny Pitt, Madhu Parasarathy,
Mahesh Viswanathan, Margaret Fleck, Shang-Hua Teng, Steve LaValle, and
especially Chandra Chekuri, Ed Reingold, and Sariel Har-Peled.
Of course this book owes a great debt to the people who taught me this
algorithms stuff in the first place: Bob Bixby and Michael Pearlman at Rice;
David Eppstein, Dan Hirschberg, and George Lueker at Irvine; and Abhiram
Ranade, Dick Karp, Manuel Blum, Mike Luby, and Raimund Seidel at Berkeley.
vi
Caveat Lector!
I stole the first iteration of the overall course structure, and the idea to write
up my own lecture notes in the first place, from Herbert Edelsbrunner; the idea
of turning a subset of my notes into a book from Steve LaValle; and several
components of the book design from Robert Ghrist.
Caveat Lector!
Of course, none of those people should be blamed for any flaws in the resulting
book. Despite many rounds of revision and editing, this book contains several
mistakes, bugs, gaffes, omissions, snafus, kludges, typos, mathos, grammaros,
thinkos, brain farts, poor design decisions, historical inaccuracies, anachronisms,
inconsistencies, exaggerations, dithering, blather, distortions, oversimplifications,
redundancy, logorrhea, nonsense, garbage, cruft, junk, and outright lies, all of
which are entirely Steve Skiena’s fault.
I maintain an issue tracker at https://github.com/jeffgerickson/algorithms,
where readers like you can submit bug reports, feature requests, and general
feedback on the book. Please let me know if you find an error of any kind,
whether mathematical, grammatical, historical, typographical, cultural, or
otherwise, whether in the main text, in the exercises, or in my other course
materials. (Steve is unlikely to care.) Of course, all other feedback is also
welcome!
Enjoy!
— Jeff
It is traditional for the author to magnanimously accept the blame for whatever
de�ciencies remain. I don’t. Any errors, de�ciencies, 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, The Algorithm Design Manual (����)
vii
Table of Contents
Preface i
About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i
Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i
Additional References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii
About the Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv
Steal This Book! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vi
Caveat Lector! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
Table of Contents ix
� Introduction �
. What is an algorithm? . . . . . . . . . . . . . . . . . . . . . . . . . .
. Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ix
T���� �� C�������
� Recursion ��
. Reductions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. Simplify and Delegate . . . . . . . . . . . . . . . . . . . . . . . . . .
. Tower of Hanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. Mergesort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Correctness • Analysis
. Quicksort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Correctness • Analysis
. The Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. Recursion Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
™Ignoring Floors and Ceilings Is Okay, Honest
. Linear-Time Selection . . . . . . . . . . . . . . . . .
™
. . . . . . . . .
Quickselect • Good pivots • Analysis • Sanity Checking
. Fast Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. Exponentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
� Backtracking ��
. N Queens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. Game Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. Subset Sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Correctness • Analysis • Variants
. The General Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . .
. Text Segmentation (Interpunctio Verborum) . . . . . . . . . . . . .
Index Formulation • ™Analysis • Variants
. Longest Increasing Subsequence . . . . . . . . . . . . . . . . . . . .
. Longest Increasing Subsequence, Take . . . . . . . . . . . . . . .
. Optimal Binary Search Trees . . . . . . . . . . . . . . . . . . . . . .
™Analysis
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
� Dynamic Programming ��
x
Table of Contents
. Mātrāvr.tta . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Backtracking Can Be Slow • Memo(r)ization: Remember Everything • Dy-
namic Programming: Fill Deliberately • Don’t Remember Everything After
All
. ™
Aside: Even Faster Fibonacci Numbers . . . . . . . . . . . . . . .
Whoa! Not so fast!
. Interpunctio Verborum Redux . . . . . . . . . . . . . . . . . . . . . .
. The Pattern: Smart Recursion . . . . . . . . . . . . . . . . . . . . .
. Warning: Greed is Stupid . . . . . . . . . . . . . . . . . . . . . . . .
. Longest Increasing Subsequence . . . . . . . . . . . . . . . . . . . .
First Recurrence: Is This Next? • Second Recurrence: What’s Next?
. Edit Distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Recursive Structure • Recurrence • Dynamic Programming
. Subset Sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. Optimal Binary Search Trees . . . . . . . . . . . . . . . . . . . . . .
. Dynamic Programming on Trees . . . . . . . . . . . . . . . . . . . .
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xi
T���� �� C�������
xii
Table of Contents
�� NP-Hardness ���
. A Game You Can’t Win . . . . . . . . . . . . . . . . . . . . . . . . . .
. P versus NP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. NP-hard, NP-easy, and NP-complete . . . . . . . . . . . . . . . . . .
. ™Formal Definitions (HC SVNT DRACONES) . . . . . . . . . . . . .
. Reductions and S . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. S (from C S ). . . . . . . . . . . . . . . . . . . . . . . . .
. Maximum Independent Set (from S ) . . . . . . . . . . . . . . .
. The General Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . .
. Clique and Vertex Cover (from Independent Set) . . . . . . . . .
. Graph Coloring (from S ) . . . . . . . . . . . . . . . . . . . . . .
. Hamiltonian Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . .
From Vertex Cover • From S • Variants and Extensions
. Subset Sum (from Vertex Cover) . . . . . . . . . . . . . . . . . . . .
xiii
T���� �� C�������
Caveat Reductor!
. Other Useful NP-hard Problems . . . . . . . . . . . . . . . . . . . .
. Choosing the Right Problem . . . . . . . . . . . . . . . . . . . . . .
. A Frivolous Real-World Example . . . . . . . . . . . . . . . . . . . .
. ™
On Beyond Zebra . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Polynomial Space • Exponential Time • Excelsior!
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Index ���
Colophon ���
xiv