In one of our current exercises we have to prove different properties belonging to distance matrices as base of binary trees. Additionally I tried to develop an algorithm for creating such a tree, given a distance matrix.

A distance matrix represents the dissimilarity of samples (for example genes), so that the number in the i-th row j-th column is the distance between element i and j. To generate a tree of it, it is necessary to determine some attributes of the distance between two elements so that it is a metric:

- (distances are positive)
- (elements with distance 0 are identical, dissimilar elements have distances greater than 0)
- (symmetry)
- (triangle inequality)

Examples for valid metrics are the euclidean distance , or the manhattan distance .

The following procedure is called hierarchical clustering, we try to combine single objects to cluster. At the beginning we start with cluster, each of them containing only one element, the intersection of this set is empty and the union contains all elements that should be clustered.

The algorithm now searches for the smallest distance in that is not 0 and merges the associated clusters to a new one containing all elements of both. After that step the distance matrix should be adjusted, because two elements are removed and a new one is added. The distances of the new cluster to all others can be computed with the following formula:

are two clusters that should be merged, represents another cluster. The constants depend on the cluster method to use, shown in table 1.

Method | $$\alpha$$ | $$\beta$$ | $$\gamma$$ | $$\delta$$ |
---|---|---|---|---|

Single linkage | 0.5 | 0.5 | 0 | -0.5 |

Complete linkage | 0.5 | 0.5 | 0 | 0.5 |

Average linkage | 0.5 | 0.5 | 0 | 0 |

Average linkage (weighted) | $$\frac{|X|}{|X| + |Y|}$$ | $$\frac{|Y|}{|X| + |Y|}$$ | 0 | 0 |

Centroid | $$\frac{|X|}{|X| + |Y|}$$ | $$\frac{|Y|}{|X| + |Y|}$$ | $$-\frac{|X|\cdot|Y|}{(|X| + |Y|)^2}$$ | 0 |

Median | 0.5 | 0.5 | -0.25 | 0 |

Here denotes the number of elements in cluster .

The algorithm continues with searching for the smallest distance in the new distance matrix and will merge the next two similar elements until just one element is remaining.

Merging of two clusters in tree-view means the construction of a parent node with both clusters as children. The first clusters containing just one element are leafs, the last node is the root of the tree.

## Small example

Let’s create a small example from the distance matrix containing 5 clusters, see table 2.

A | B | C | D | E | |
---|---|---|---|---|---|

A | 0 | 5 | 2 | 1 |
6 |

B | 5 | 0 | 3 | 4 | 1.5 |

C | 2 | 3 | 0 | 1.5 | 4 |

D | 1 |
4 | 1.5 | 0 | 5 |

E | 6 | 1.5 | 4 | 5 | 0 |

A and D are obviously the most similar elements in this matrix, so we merge them. To make the calculation easier we take the average linkage method to compute the new distances to other clusters:

With these values we are able to construct the new distance matrix of 4 remaining clusters, shown in table 3.

A,D | B | C | E | |
---|---|---|---|---|

A,D | 0 | 4.5 | 1.75 | 5.5 |

B | 4.5 | 0 | 3 | 1.5 |

C | 1.75 | 3 | 0 | 4 |

E | 5.5 | 1.5 |
4 | 0 |

This matrix gives us the next candidates for clustring, B and E with a distance of 1.5.

With the appropriate distance matrix of table 4.

A,D | B,E | C | |
---|---|---|---|

A,D | 0 | 5 | 1.75 |

B,E | 5 | 0 | 3.5 |

C | 1.75 |
3.5 | 0 |

Easy to see, now we cluster [A+D] with C:

and obtain a last distance matrix with table 5.

A,C,D | B,E | |
---|---|---|

A,C,D | 0 | 4.25 |

B,E | 4.25 |
0 |

Needless to say, further calculations are trivial. There are only to clusters left and the combination of them gives us the final cluster containing all elements and the root of the desired tree.

The final tree is shown in figure 1. You see, it is not that difficult as expected and ends in a beautiful image!

## Remarks

If is defined as above there is no guarantee that edge weights reflect correct distances! When you calculate the weights in my little example you’ll see what I mean. If this property is desired the distance function has to comply with the condition of ultrametric inequality: .

The method described above is formally known as **agglomerative clustering**, merging smaller clusters to a bigger one. There is another procedure that splits bigger clusters into smaller ones, starting with a cluster that contains all samples. This method is called **divisive clustering**.

## Post a comment

read more about submitting comments

`_comments`

directory and send me a pull request