# The Algorithm of Natural Selection

In Darwin’s Dangerous Idea, Daniel Dennett has described natural selection as an algorithmic process. In this post, I want to look at what this algorithm is. But let me begin with an introduction to algorithms in general. We normally think of an algorithm as a set of instructions for completing a task. A recipe, for example, is an algorithm of sorts. But we usually use the term with respect to computer programs. A computer completes various tasks by following a set of instructions given to it in a programming language. Note that the algorithm is not identical with the specific set of instructions given. Different code can express the same algorithm. Here are two examples of code which express the same algorithm:

```10 print "Hello World!"
20 goto 10```

and

```while (1) {
printf("Hello World!\n");
};```

The first is in BASIC, and the second is what the same algorithm looks like in C or PHP. The result of each piece of code is to repeatedly print a line of text that says “Hello World!”.

More precisely, each piece of code corresponds to a different algorithm at the machine level. Even when the same code is used by two different languages, C and PHP in this case, the underlying algorithms used to run the code at the machine level are different. But those details are usually unimportant to the programmer. The programmer normally concerns himself with an algorithm at a higher level of abstraction, one at which the same algorithm may be expressed differently by different languages or with different code in the same language. For example, here’s another way to express the same algorithm illustrated already in PHP:

```do {
echo "Hello World!\n";
continue;
} while (false);```

The important thing to note is that we can understand how these different pieces of code can express the same algorithm without understanding the details, which also involve algorithms, of how it gets executed.

Computers normally run many more advanced algorithms than the one used in the illustration here. When you view video on a website, this is made possible by detailed and complicated algorithms that translate the contents of a computer file to images on your monitor and sound in your speakers. When you surf the web, you rely on algorithms. When you scroll the text on this page (or page down), you are relying on algorithms to get things done for you. Short of using your computer as a paperweight or a footstool, there is little you do with it that does not make use of algorithms. A computer is a machine designed specifically for running algorithms, and living in the computer age, you are well aware of many of the amazing things computers do by running algorithms.

Now you may be thinking, especially if you’re of a creationist bent, that if natural selection is an algorithm, it must have a programmer, just as all the algorithms running on your computer have programmers. I hope to explain the algorithm of natural selection in enough detail that you will understand why it doesn’t require a programmer.

Let’s begin with the tautology that, other things being equal, more stable things outlast less stable things. The more stable something is, the longer it is likely to last. Given a universe full of unstable things, things are going to get more stable over time. Unstable things don’t last, and when they cease to be, their components fall into different arrangements, whether with each other or with other things. Most of the configurations that things fall into may be unstable, and those won’t last. But when things fall into stable configurations, however rare that may be, they will last longer. Given enough time, more and more things will fall into stable configurations. Given that stable things last, the stable things will accumulate. And given that unstable things do not last, the unstable things will not accumulate. So, the overall trend is for stability to increase in the universe, and this is by an unprogrammed algorithmic process that is grounded in the logic of stability.

Once there are a sufficient number of stable things in the universe, these stable things will become building blocks for the formation of new things. Again, many of the formations that these building blocks fall into may well be unstable. But the stable ones, however rare they are to happen, will accumulate. This will eventually produce a new level of larger building blocks. The same procedure will repeat over and over, and given enough time, there will be countless stable things at multiple scales. The biggest stable things we know of in the universe are galactic clusters, which are made up of galaxies, which are made up of solar systems, which are made up of stars and planets, etc. The smallest building blocks we know of are subatomic particles, such as quarks. Protons, electrons and neutrons are made up of quarks. Atoms are made up of protons, electrons, and neutrons. Molecules are made from atoms. Larger pieces of matter are made of molecules. And the things we see around us in the world are composed of various, often complex, arrangements of these larger pieces of matter.

So far, we have been looking at self-sustaining things, arrangements of matter that are naturally stable. Given an unstable universe, things keep falling into different arrangements. Given enough time, some arrangements of matter pop up that have the ability to replicate themselves by drawing on nearby resources. However rare this is to happen, it only needs to happen once. Once there is one thing that can replicate itself, the copies it makes of itself will also be able to replicate themselves. And so on. Even if the replicators do not last much longer than it takes to make new copies of themselves, copies of them will spread around, making more copies, and so on, so that even far in the future, there may be multiple-generation copies of them. This process follows from the logic of replication, and like the tendency toward stability, it needs no programmer. The process of copying oneself is one that generations of replicators can repeat indefinitely.

Although some, even many, replicators may make perfect copies of themselves, some may make imperfect copies of themselves. This introduces variation in the design on a replicator. In some instances, it may prevent the copy from being able to replicate. But when this happens, the change will not spread to new generations. In other instances, it will not impair the ability to replicate, and the change will spread to multiple generations. In some instances, this change in design will give the new replicators who have it an advantage over other replicators. Since the replicators will be competing for limited resources in their local area, those with an advantage will tend to out-replicate those without it. Thus, improvements to a replicator’s design will spread more than impairments to a replicator’s design will. Even if most copying errors are more harmful than helpful, it is mainly the helpful ones that will spread. So, among replicators, there is a general trend toward spreading new properties that help them replicate better, and the improvements to design, but not the impairments to design, will accumulate over time.

There are two general improvements that can enhance a replicator’s ability to replicate itself. One is to speed up the rate of replication, and the other is to increase the longevity of the replicator, so that it has more time to make more copies of itself. One way to speed up replication is to increase the ability to assimilate resources, such as making the replicator actively respond to the presence of resources by moving nearer to them or by grabbing ahold of them. This involves a primitive stimulus-response system that can detect potential resources and take appropriate action regarding them. Given that some replicators will try to assimilate other replicators, one way to increase the longevity of a replicator is to steer it away from replicators that would assimilate it. This also makes use of a stimulus-response system. In both cases, it helps a replicator when it can direct its actions based on cues from its environment. So, when copying errors led to replicators with such a stimulus-response system, it gave an important advantage to the replicators who had it and got copied more.