Richard Dawkins is an evolutionary biologist and author of many science books. In *The Blind Watchmaker* he explains how complex systems can exist without the need of an intelligent design.

Chapter 10 of that book delves into the tree of life. He argues that the tree of life is not arbitrary taxonomy like the classification of animals into kingdoms or families, but it is more like a family tree, where the branching of the tree uniquely describes the true ancestry relationship between the nodes.

Even though we made great strides in genetics and mapped the DNA from several different species, determining the structure of the tree is very difficult. First, we need to define a suitable metric that would encode the ancestry proximity of two species. In other words, if species A evolved into B and C, we need a metric that would lead us to link A-B and A-C but not B-C. Another problem is that internal nodes can be missing (e.g. ancestor species went extinct without fossils).

In this post we’ll deal with a much simpler version of this problem, in which we have the metric well defined, we know the distance between every pair of nodes (perfect information), and all our nodes are leaves, so we have the freedom to decide the internal nodes of the tree.

This simplified problem can be formalized as follows:

**Constructing a tree for its distance matrix problem.** Suppose we are given a `n x n`

distance matrix `D`

. Construct a tree with `n`

leaves such that the distance between every pair of leaves can be represented by `D`

.

To reduce the amount of possible solutions, we will assume a canonical representation of a tree. A canonical tree doesn’t have any nodes with degree 2. We can always reduce a tree with nodes with degree 2 into a canonical one. For example:

### Terminology

Let’s introduce the terminology necessary to define the algorithm for solving our problem. A **distance matrix** `D`

is a square matrix where `d_ij`

represents the distance between elements `i`

and `j`

. This matrix is symmetric (`d_ij = d_ji`

), all off-diagonal entries are positive, the diagonal entries are 0, and a triplet `(i, j, k)`

satisfy the triangle inequality, that is,

`d_ik <= d_ij + d_jk`

A distance matrix is **additive** if there is a solution to the problem above.

We say two leaves are **neighbors** if they share a common parent. An edge connecting a leaf to its parent is called **limb** (edges connecting internal nodes are not limbs).

### Deciding whether a matrix is additive

We can decide whether a matrix is additive via the 4-point theorem:

**Four-point Theorem.** Let D be a distance matrix. If, for every possible set of 4 indexes `(i, j, k, l)`

, the following inequality holds (for some permutation):

(1) `d_ij + d_kl <= d_ik + d_jl = d_il + d_jk`

then D is *additive*.

*Sketch of proof.* We can derive the general idea from the example tree below:

We can see by inspection that (1) is true by inspecting the edges on the path between each pair of leaves. This will be our base case for induction.

Now, we’ll show that if we’re given a distance matrix satisfying (1), we are able to reconstruct a valid tree from it. We have that `d_ik = a + e + c`

, `d_jl = b + e + d`

, `d_ij = a + b`

and `d_kl = c + d`

. If we add the first two terms and subtract the last two, we have `d_ik + d_jl - d_ij + d_kl = 2e`

, so we have

`e = (d_ik + d_jl - d_ij + d_kl) / 2`

We know from (1) that `d_ik + d_jl >= d_kl + d_ij > d_kl`

, so `e`

is positive.

If we add `d_ik`

and `d_ij`

and subtract `d_jl`

, we get `d_ik + d_ij - d_jk = 2a`

, so

`a = (d_ik + d_ij - d_jk) / 2`

To show that a is positive, we need to remember that a distance matrix satisfy the triangle inequality, that is, for any three nodes, `x`

, `y`

, `z`

, `d_xy + d_yz >= d_xz`

. In our case, this means `d_ij + d_jk >= d_ik`

, hence `d_ij >= d_ik - d_jk`

and a is positive. We can use analogous ideas to derive the values for `b`

, `c`

and `d`

.

To show this for the more general case, if we can show that for every possible set of 4 leaves `(i, j, k, l)`

this property is held, then we can show there’s a permutation of these four leaves such that the tree from the induced paths between each pair of leaves looks like the specific example we showed above.

For at least one one of these quadruplets, `i`

and `j`

will be neighbors in the reconstructed tree. With the computed values of `a`

, `b`

, `c`

, `d`

, `e`

, we are able to merge `i`

and `j`

into its parent and generate the distance matrix for `n-1`

leaves, which we can keep doing until `n = 4`

. We still need to prove that this modified `n-1 x n-1`

satisfies the 4-point theorem if and only if the `n x n`

does.

### Limb cutting approach

We’ll see next an algorithm for constructing a tree from an additive matrix.

The general idea is that even though we don’t know the structure of the tree that will “yield” our additive matrix, we are able to determine the length of the limb of any leaf. Knowing that, we can remove the corresponding leaf (and limb) from our unknown tree by removing the corresponding row and column in the distance matrix. We can then solve for the `n-1 x n-1`

case. Once we have the solution (a tree) for the smaller problem we can “attach” the leaf into the tree.

To compute the limb length and where to attach it, we can rely on the following theorem.

**Limb Length Theorem**: Given an additive matrix `D`

and a leaf `j`

, `limbLength(j)`

is equal to the minimum of

`(2) (d_ij + d_jk - d_ik)/2`

over all pairs of leaves `i`

and `k`

.

The idea behind the theorem is that if we remove `parent(j)`

from the unknown tree, it will divide it into at least 3 subtrees (one being leaf `j`

on its own). This means that there exists leaves `i`

and `k`

that are in different subtrees. This tells us that the path from `i`

to `k`

has to go through `parent(j)`

and also that the path from `i`

to `j`

and from `j`

to `k`

are disjoint except for `j`

‘s limb, so we can conclude that:

`d_ik = d_ij + d_jk - 2*limbLength(j)`

which yields (2) for `limbLength(j)`

. We can show now that for `i`

and `k`

on the same subtree `d_ik <= d_ij + d_jk - 2*limbLength(j)`

, and hence

`limbLength(j) <= (d_ij + d_jk - d_ik)/2`

This means that finding the minimum of (2) will satisfy these constraints.

**Attaching leaf j back in.** From the argument above, there are at least one pair of leaves `(i, k)`

that yields the minimum `limbLength(j)`

that belongs to different subtrees when `parent(j)`

is removed. This means that `parent(j)`

lies in the path between `i`

and `k`

. We need to plug in `j`

at some point on this path such that when computing the distance from `j`

to `i`

and from `j`

to `k`

, it will yield `d_ij`

and `d_jk`

respectively. This might fall in the middle of an edge, in which case we need to create a new node. Note that as long as the edges all have positive values, there’s only one location within the path from `i`

to `k`

that we can attach `j`

.

Note: There’s a missing detail in the induction argument here. How can we guarantee that no matter what tree is returned from the inductive step, it is such that attaching `j`

will yield consistent distances from `j`

to all other leaves besides `i`

and `k`

?

This constructive proof gives us an algorithm to find a tree for an additive matrix.

**Runtime complexity.** Finding `limbLength(j)`

takes `O(n^2)`

time since we need to inspect every pair of entries in `D`

. We can generate an `n-1 x n-1`

matrix in `O(n^2)`

and find the attachment point in `O(n)`

. Since each recursive step is proportional to the size of the matrix and we have n such steps, the total runtime complexity is `O(n^3)`

.

**Detecting non-additive matrices.** If we find a non-positive `limbLength(j)`

, this condition is sufficient for a matrix to be considered non-additive, since if we have a corresponding tree we know it has to represent the length of j’s limb. However, is this necessary? It could be that we find a positive value for `limbLength(j)`

but when trying to attach j back in the distances won’t match.

The answer to this question goes back to the missing detail on the induction step and I don’t know how to answer.

### The Neighbor-Joining Algorithm

Naruya Saitou and Masatoshi Nei developed an algorithm, called **Neighbor Joining**, that also constructs a tree from an additive matrix, but has the additional property that for non-additive ones it serves as heuristic.

The idea behind is simple: It transforms the distance matrix D into another n x n matrix, `D*`

, such that the minimum non-diagonal entry, say `d*_ij`

, in that matrix corresponds to neighboring vertices `(i ,j)`

in the tree, which is generally not true for a distance matrix.

The proof that D* has this property is non-trivial and will not provide here. Chapter 7 of [1] has more details and the proof.

Given this property, we can find `i`

and `j`

such that `d*_ij`

is minimal and compute the limbs distances `limbLength(i)`

and `limbLength(j)`

, replace them with a single leaf `m`

, and solve the problem recursively. With the tree returned by the recursive step we can then attach `i`

and `j`

into `m`

, which will become their parents.

### Conclusion

In this post we saw how to construct a tree from the distance between its leaves. The algorithms are relatively simple, but proving that they work is not. I got the general idea of the proofs but didn’t get with 100% of detail.

The idea of reconstructing the genealogical tree of all the species is fascinating and is a very interesting application of graph theory.

### References

[1] *Bioinformatics Algorithms: An Active Learning Approach* – Compeau, P. and Pevzner P. – Chapter 10

[2] *The Blink Watchmaker* – Richard Dawkins