This HTML version of Think Complexity, 2nd Edition is provided for convenience, but it is not the best format of the book.
In particular, some of the symbols are not rendered correctly.
You might prefer to read the PDF version.
Chapter 0 Preface
This book is primarily about complexity science, but it is also
about data structures and algorithms, intermediate
programming in Python, computational modeling, and the philosophy of
- Complexity science
- Complexity is an interdisciplinary
field—at the intersection of mathematics, computer science and
natural science—that focuses on discrete models of physical and
social systems. In particular, it focuses on complex systems,
which are systems with many interacting components.
- Data structures:
- A data structure is a
collection of data elements organized in a way that supports
particular operations. For example, a Python dictionary organizes
key-value pairs in a way that provides fast mapping from keys to
values, but mapping from values to keys is slower.
- An algorithm is a process for performing a
computation. Designing efficient programs often involves the
co-evolution of data structures and the algorithms that use them.
For example, in the first few chapters I present graphs, data
structures that implement graphs, and graph algorithms based on
those data structures.
- Computational modeling:
- A model is a simplified
description of a system used for simulation or analysis.
Computational models are designed to take advantage of cheap, fast
- Philosophy of science:
- The experiments and results in this book
raise questions relevant to the philosophy of science,
including the nature of scientific laws, theory choice, realism and
instrumentalism, holism and reductionism, and epistemology.
Complex systems include networks and graphs, cellular automatons,
agent-based models and swarms, fractals and self-organizing
systems, chaotic systems and cybernetic systems. These terms
might not mean much to you at this point. We will get to them
soon, but you can get a preview at
I hope this book helps readers explore a set of topics
and ideas they might not encounter otherwise, practice programming
skills in Python, and learn more about data structures and
algorithms (or review material that might have been less engaging
the first time around).
Features of this book include:
- Technical details
- There are many books about complex systems,
but most are written for a popular audience. They usually skip the
technical details, which is frustrating for people who can handle
it. This book presents the mathematics, the code, and the other
material you need to really understand this work.
- Further reading
- Throughout the book, I include pointers to
further reading, including original papers (most of which are
available electronically) and related articles from Wikipedia and
other sources. Some professors have an allergic reaction to
Wikipedia, on the grounds that students depend too heavily on a
suppposedly unreliable source. Since many of my references are
Wikipedia articles, I want to explain my thinking. First, the
articles on complexity science and related topics tend to be good;
second, they are written at a level that is accessible after you
have read this book (but sometimes not before); and finally, they
are freely available to readers all over the world. If there is a
danger in sending readers to these references, it is not that the
material is unreliable, but that the readers won’t come back!
- Jupyter notebooks
- For each chapter I provide a Jupyter notebook
that includes the code from the chapter, additional examples (and
especially animations), and suggestions for experiments you can run
with small changes in the code.
- Exercises and solutions
- At the end of each chapter I suggest
exercises you might want to work on, and I include solutions.
0.1 Who is this book for?
The examples and supporting code for this book are in Python. You
should know core Python and you should be
familiar with object-oriented features, at least using objects if not
defining your own.
If you are not already familiar with Python, you might want to start
with my other book, Think Python, which is an introduction to
Python for people who have never programmed, or Mark
Lutz’s Learning Python, which might be better for people with
I use NumPy and SciPy extensively. If you are familiar with them
already, that’s great, but I will also explain the functions
and data structures I use.
I assume that the reader knows basic mathematics, including complex
numbers. You don’t need much calculus; if you understand the concepts
of integration and differentiation, that will do.
I use some linear algebra, but I will explain it as we
0.2 Using the code
All code used in this book is available from
If you are not familiar with Git, it is a
version control system that allows you to keep track of the files that
make up a project. A collection of files under Git’s control is
called a “repository”. GitHub is a hosting service that provides
storage for Git repositories and a convenient web interface.
The GitHub homepage for my repository provides several ways to
work with the code:
- You can create a copy of my repository
on GitHub by pressing the Fork button. If you don’t already
have a GitHub account, you’ll need to create one. After forking, you’ll
have your own repository on GitHub that you can use to keep track
of code you write while working on this book. Then you can
clone the repo, which means that you copy the files
to your computer.
- Or you can clone my repository. You don’t need a GitHub account
to do this, but you won’t be able to write your changes back to
- If you don’t want to use Git at all, you can download the files
in a Zip file using the green button that says “Clone or download”.
All of the code is written to work in both Python 2 and Python 3
with no translation.
I developed this book using Anaconda from Continuum Analytics, which
is a free Python distribution that includes all the packages you’ll
need to run the code (and lots more). I found Anaconda easy to
install. By default it does a user-level installation, not
system-level, so you don’t need administrative privileges. And it
supports both Python 2 and Python 3. You can download Anaconda from
If you don’t want to use Anaconda, you will need the following
Although these are commonly used packages, they are not included with
all Python installations, and they can be hard to install in some
environments. If you have trouble installing them, I
recommend using Anaconda or one of the other Python distributions
that include these packages.
The repository includes Python scripts and several Jupyter
notebooks. If you have not used Jupyter before, you can read about
it at http://jupyter.org.
There are three ways you can work with the Jupyter notebooks:
- Run Jupyter on your computer
If you installed Anaconda, you probably got Jupyter by default. To
check, start the server from the command line, like this:
$ jupyter notebook
If it’s not installed, you can install it using
conda, which is
the package manager used by Anaconda:
$ conda install jupyter
When you start the server, it should launch your default web browser
or create a new tab in an open browser window.
- Run Jupyter on Binder
Binder is a service that runs Jupyter in a virtual machine. If you
follow this link, http://mybinder.org/repo/AllenDowney/ThinkComplexity2,
you should get a Jupyter home page with the notebooks for this book
and the supporting data and scripts.
You can run the scripts and modify them to run your own code, but the
virtual machine you run them in is temporary. Any changes you make will
disappear, along with the virtual machine, if you leave it idle for
more than about an hour.
- View notebooks on GitHub
GitHub provides a view of the notebooks you can
can use to read the notebooks and see the results I
generated, but you won’t be able to modify or run the code.
And in the later chapters you won’t see the animations move.
Good luck, and have fun!
Allen B. Downey
Professor of Computer Science
Olin College of Engineering
If you have a suggestion or correction, please send email to
firstname.lastname@example.org. If I make a change based on your
feedback, I will add you to the contributor list
(unless you ask to be omitted).
If you include at least part of the sentence the
error appears in, that makes it easy for me to search. Page and
section numbers are fine, too, but not quite as easy to work with.
- John Harley, Jeff Stanton, Colden Rouleau and
Keerthik Omanakuttan are Computational Modeling students who
pointed out typos.
- Jose Oscar Mur-Miranda found several typos.
- Phillip Loh, Corey Dolphin, Noam Rubin and Julian Ceipek
found typos and made helpful suggestions.
- Sebastian Schöner sent two pages of corrections!
- Philipp Marek sent a number of corrections.
Other people who reported errors include
Muhammad Najmi bin Ahmad Zabidi,
Alex Hantman, and
Are you using one of our books in a class? We'd like to know
about it. Please consider filling out this short survey.
Think Python 2e
Think Stats 2e