2014 Bookmatter PythonAlgorithms
2014 Bookmatter PythonAlgorithms
Second Edition
■■Chapter 1: Introduction�����������������������������������������������������������������������������������������������������1
v
■ Contents at a Glance
Index���������������������������������������������������������������������������������������������������������������������������������289
vi
Contents
■■Chapter 1: Introduction�����������������������������������������������������������������������������������������������������1
What’s All This, Then?��������������������������������������������������������������������������������������������������������������������2
What the book is about:����������������������������������������������������������������������������������������������������������������������������������������� 3
What the book covers only briefly or partially:������������������������������������������������������������������������������������������������������ 3
What the book isn’t about:������������������������������������������������������������������������������������������������������������������������������������� 3
vii
■ Contents
Summary�������������������������������������������������������������������������������������������������������������������������������������38
If You’re Curious …���������������������������������������������������������������������������������������������������������������������39
Exercises�������������������������������������������������������������������������������������������������������������������������������������39
References����������������������������������������������������������������������������������������������������������������������������������40
viii
■ Contents
Stronger Assumptions�����������������������������������������������������������������������������������������������������������������85
Invariants and Correctness���������������������������������������������������������������������������������������������������������86
Relaxation and Gradual Improvement�����������������������������������������������������������������������������������������87
Reduction + Contraposition = Hardness Proof����������������������������������������������������������������������������88
Problem Solving Advice���������������������������������������������������������������������������������������������������������������89
Summary�������������������������������������������������������������������������������������������������������������������������������������90
If You’re Curious ...����������������������������������������������������������������������������������������������������������������������90
Exercises�������������������������������������������������������������������������������������������������������������������������������������91
References����������������������������������������������������������������������������������������������������������������������������������92
Go Deep!������������������������������������������������������������������������������������������������������������������������������������102
Depth-First Timestamps and Topological Sorting (Again)���������������������������������������������������������������������������������� 104
Sorting by Halves����������������������������������������������������������������������������������������������������������������������125
How Fast Can We Sort?������������������������������������������������������������������������������������������������������������������������������������� 127
Huffman’s Algorithm������������������������������������������������������������������������������������������������������������������144
The Algorithm���������������������������������������������������������������������������������������������������������������������������������������������������� 145
The First Greedy Choice������������������������������������������������������������������������������������������������������������������������������������� 147
Going the Rest of the Way��������������������������������������������������������������������������������������������������������������������������������� 147
Optimal Merging������������������������������������������������������������������������������������������������������������������������������������������������ 148
x
■ Contents
Summary�����������������������������������������������������������������������������������������������������������������������������������159
If You’re Curious …�������������������������������������������������������������������������������������������������������������������160
Exercises�����������������������������������������������������������������������������������������������������������������������������������160
References��������������������������������������������������������������������������������������������������������������������������������161
xi
■ Contents
Far-Fetched Subproblems���������������������������������������������������������������������������������������������������������198
Meeting in the Middle���������������������������������������������������������������������������������������������������������������201
Knowing Where You’re Going����������������������������������������������������������������������������������������������������203
Summary�����������������������������������������������������������������������������������������������������������������������������������206
If You’re Curious ...��������������������������������������������������������������������������������������������������������������������207
Exercises�����������������������������������������������������������������������������������������������������������������������������������207
References��������������������������������������������������������������������������������������������������������������������������������208
Index���������������������������������������������������������������������������������������������������������������������������������289
xiii
About the Author
xv
About the Technical Reviewer
Stefan Turalski is just another coder who is perfectly happy delivering pragmatic,
not necessarily software, solutions and climbing the impassable learning curve.
He has more than a decade of experience building solutions in such diverse
domains as knowledge management, embedded networking, healthcare, power
and gas trading, and, in the last few years, finance.
Focusing on code optimization and systems integration, he has dabbled
(or almost drowned) in quite a few programming languages and has abused a number
of open source and commercial software frameworks, libraries, servers, and so on.
Stefan is currently working on a highly scalable, low-latency, intraday risk
valuation system at a financial institution in London. His latest interests revolve
around functional and reactive programming, F#, Clojure, Python, OpenCL,
and WebGL.
He still cannot believe that he was trusted enough to help on the second
edition of Magnus Lie Hetland’s superb book. Stefan hopes that his (and your) brain
cells injured while studying the algorithmic problems covered by the author will
recover stronger and wiser!
xvii
Acknowledgments
Thanks to everyone who contributed to this book, either directly or indirectly. This certainly includes my algorithm
mentors, Arne Halaas and Bjørn Olstad, as well as the entire crew at Apress and my brilliant tech editors, Alex Martelli
(for the first edition) and Stefan Turalski. Thanks to all the readers who pointed out errors in the first edition; I hope
I have corrected most of them. I’d especially like to thank Gerald Senarclens de Grancy, who supplied an extensive,
well-annotated list of errata covering the entire book. Thanks to Nils Grimsmo, Jon Marius Venstad, Ole Edsberg,
Rolv Seehuus, and Jorg Rødsjø for useful input; to my girlfriend, Janne Varvára Seem, my parents, Kjersti Lie and Tor
M. Hetland, and my sister, Anne Lie-Hetland, for their interest and support; and to my uncle Axel, for checking my
French. Finally, a big thank-you to the Python Software Foundation for their permission to reproduce parts of the
Python standard library and to Randall Munroe for letting me include some of his wonderful XKCD comics.
xix
Preface
This book is a marriage of three of my passions: algorithms, Python programming, and explaining things. To me, all
three of these are about aesthetics—finding just the right way of doing something, looking until you uncover a hint
of elegance, and then polishing that until it shines (or at least until it is a bit shinier). Of course, when there’s a lot of
material to cover, you may not get to polish things quite as much as you want. Luckily, though, most of the content in
this book is prepolished because I’m writing about really beautiful algorithms and proofs, as well as one of the cutest
programming languages out there. As for the third part, I’ve tried hard to find explanations that will make things seem
as obvious as possible. Even so, I’m sure I have failed in many ways, and if you have suggestions for improving the
book, I’d be happy to hear from you. Who knows, maybe some of your ideas could make it into a future edition. For
now, though, I hope you have fun with what’s here and that you take any newfound insight and run with it. If you can,
use it to make the world a more awesome place, in whatever way seems right.
xxi