Python Algorithms: Mastering Basic Algorithms in the
Python Language
Download
Introduction
What’s in This Book
The book is structured as follows:
Chapter 1: Introduction. You’ve already gotten through most of this. It gives an overview of the book.
Chapter 2: The Basics. This covers the basic concepts and terminology, as well as some fundamental
math. Among other things, you learn how to be sloppier with your formulas than ever before, and still
get the right results, with asymptotic notation.
Chapter 3: Counting 101. More math—but it’s really fun math, I promise! There’s some basic
combinatorics for analyzing the running time of algorithms, as well as a gentle introduction to recursion
and recurrence relations.
Chapter 4: Induction and Recursion … and Reduction. The three terms in the title are crucial, and they
are closely related. Here we work with induction and recursion, which are virtually mirror images of each
other, both for designing new algorithms and for proving correctness. We also have a somewhat briefer
look at the idea of reduction, which runs as a common thread through almost all algorithmic work.
Chapter 5: Traversal: A Skeleton Key to Algorithmics. Traversal can be understood using the ideas of
induction and recursion, but it is in many ways a more concrete and specific technique. Several of the
algorithms in this book are simply augmented traversals, so mastering traversal will give you a real
jump start.
Chapter 6: Divide, Combine, and Conquer. When problems can be decomposed into independent
subproblems, you can recursively solve these subproblems and usually get efficient, correct algorithms
as a result. This principle has several applications, not all of which are entirely obvious, and it is a mental
tool well worth acquiring.
Chapter 7: Greed is Good? Prove It! Greedy algorithms are usually easy to construct. One can even
formulate a general scheme that most, if not all, greedy algorithms follow, yielding a plug-and-play
solution. Not only are they easy to construct, but they are usually very efficient. The problem is, it can be
hard to show that they are correct (and often they aren’t). This chapter deals with some well-known
examples and some more general methods for constructing correctness proofs.
Chapter 8: Tangled Dependencies and Memoization. This chapter is about the design method (or,
historically, the problem) called, somewhat confusingly, dynamic programming. It is an advanced
technique that can be hard to master but that also yields some of the most enduring insights and elegant
solutions in the field.
Chapter 9: From A to B with Edsger and Friends. Rather than the design methods of the previous three
chapters, we now focus on a specific problem, with a host of applications: finding shortest paths in
networks, or graphs. There are many variations of the problem, with corresponding (beautiful)
algorithms.
Chapter 10: Matchings, Cuts, and Flows. How do you match, say, students with colleges so you
maximize total satisfaction? In an online community, how do you know whom to trust? And how do you
find the total capacity of a road network? These, and several other problems, can be solved with a small
class of closely related algorithms and are all variations of the maximum flow problem, which is covered
in this chapter.
Chapter 11: Hard Problems and (Limited) Sloppiness. As alluded to in the beginning of the
introduction, there are problems we don’t know how to solve efficiently and that we have reasons to
think won’t be solved for a long time—maybe never. In this chapter, you learn how to apply the trusty
tool of reduction in a new way: not to solve problems but to show that they are hard. Also, we have a
look at how a bit of (strictly limited) sloppiness in our optimality criteria can make problems a lot
easier to solve.
Appendix A: Pedal to the Metal: Accelerating Python. The main focus of this book is asymptotic
efficiency—making your programs scale well with problem size. However, in some cases, that may not
be enough. This appendix gives you some pointers to tools that can make your Python programs go
faster. Sometimes a lot (as in hundreds of times) faster.
Appendix B: List of Problems and Algorithms. This appendix gives you an overview of the algorithmic
problems and algorithms discussed in the book, with some extra information to help you select the right
algorithm for the problem at hand.
Appendix C: Graph Terminology and Notation. Graphs are a really useful structure, both in describing
real-world systems and in demonstrating how various algorithms work. This chapter gives you a tour of
the basic concepts and lingo, in case you haven’t dealt with graphs before.
Appendix D: Hints for Exercises. Just what the title says