Theoretical Approach to Agglomerative Clustering

Agglomerative clustering is a type of hierarchical clustering method used in data analysis, particularly in the fields of machine learning and statistics. This method involves grouping a set of objects into clusters based on their similarity. Unlike divisive clustering, which starts with all objects in one cluster and recursively splits them, agglomerative clustering starts with each object as its own cluster and merges the most similar pairs of clusters iteratively until a stopping criterion is met. The algorithm begins with each data point as an individual cluster and iteratively merges the most similar clusters until a single comprehensive cluster encompassing all data points is formed or a predefined stopping criterion is reached. This bottom-up approach contrasts with divisive clustering methods, which take a top-down approach by starting with one cluster containing all data points and recursively splitting it.

{tocify} $title={Table of Contents}


Agglomerative clustering is widely used across different domains such as bioinformatics, market research, and image analysis due to its simplicity and interpretability. The choice of distance metrics and linkage criteria are pivotal in determining the quality and characteristics of the resulting clusters. Common distance metrics include Euclidean, Manhattan, and cosine similarity, while linkage criteria such as single linkage, complete linkage, average linkage, and Ward's linkage define how the distance between clusters is calculated during the merging process. This article delves into the mathematical foundations of agglomerative clustering, exploring various distance metrics and linkage criteria, and provides a detailed examination of the algorithmic steps involved. Additionally, we discuss the computational complexity and considerations for practical implementation, aiming to equip researchers and practitioners with a thorough understanding of this versatile clustering technique.

Mathematical Foundation

Understanding the mathematical foundations of agglomerative clustering is essential for effectively applying this method to real-world data analysis problems. This section delves into the core mathematical concepts that underpin agglomerative clustering, including distance metrics and linkage criteria. These elements play a crucial role in defining how data points are grouped together and how clusters are merged during the clustering process. By exploring the various distance metrics used to measure similarity between data points and the different linkage criteria that influence the merging of clusters, we can gain deeper insights into the mechanics of agglomerative clustering and its impact on the resulting cluster structures.

Distance Metrics

The distance matrix `D` is a crucial component of agglomerative clustering. It is used to store and update the distances between all pairs of clusters throughout the clustering process. Below are the common distances used in agglomerative clustering.

1. Euclidean Distance

The Euclidean distance is the most widely used distance metric, defined as the straight-line distance between two points in a Euclidean space. For two points \( x_i = x_{i1}, x_{i2}, \ldots, x_{iz} \) and \(x_j = x_{j1}, x_{j2},..x_{jz}\) the Euclidean distance is given by:

`d(x_i, x_j) = \sqrt{\sum_{k=1}^z (x_{ik} - x_{jk})^2}`

This metric is appropriate when the data is continuous and the scale of measurement is consistent across dimensions.

2. Manhattan Distance

The Manhattan distance, also known as the L1 norm or taxicab distance, measures the absolute sum of differences across dimensions.

For points \( x_i \) and \( x_j \), it is defined as:

`d(x_i, x_j) = \sum_{k=1}^z | x_{ik} - x_{jk}|`

This metric is useful in cases where the differences in each dimension are equally important and is more robust to outliers than the Euclidean distance.

3. Cosine Similarity

Cosine similarity measures the cosine of the angle between two vectors, which is particularly useful for high-dimensional data where the magnitude of the vectors may vary significantly. It is defined as:

`d(x_i, x_j) = 1 - \frac{\sum_{k=1}^z x_{ik} x_{jk}}{\sqrt{\sum_{k=1}^z x_{ik}^2}\sqrt{\sum_{k=1}^z x_{jk}^2}}`

Cosine similarity is widely used in text mining and information retrieval, where the orientation rather than the magnitude of the vectors is of primary interest.

4. Hamming Distance

The Hamming distance is used for categorical data and measures the number of positions at which the corresponding elements of two vectors are different. For binary vectors `x_i , x_j`, it is defined as:

`d(x_i, x_j) = \sum_{k=1}^z 1 (x_{ik} - x_{jk})`

where `1(*)` is the indicator function that equals 1 if the condition is true and 0 otherwise. Hamming distance is particularly useful in error detection and correction applications.

Linkage Criterion

In agglomerative clustering, the linkage criterion determines how the distance between two clusters is calculated during the merging process. The choice of linkage criterion affects the shape and structure of the resulting clusters. Below are the common linkage criteria used in agglomerative clustering.

1. Single Linkage

Single linkage, also known as minimum linkage, defines the distance between two clusters as the minimum distance between any pair of points from the two clusters. For clusters `C_i, C_j`, the single linkage distance is given by:

`d(C_i, C_j) = min{d(a,b) : a \in C_i, b \in C_j}`

This criterion tends to produce elongated and "chained" clusters because it considers only the closest points between clusters.

2. Complete Linkage

Complete linkage, or maximum linkage, defines the distance between two clusters as the maximum distance between any pair of points from the two clusters. For clusters `C_i, C_j`, the complete linkage distance is given by:

`d(C_i, C_j) = max{d(a,b) : a \in C_i, b \in C_j}`

Complete linkage tends to create more compact clusters, as it considers the furthest points between clusters, avoiding the formation of large and loose clusters.

3. Average Linkage

Average linkage calculates the distance between two clusters as the average distance between all pairs of points from the two clusters. For clusters `C_i, C_j`, the average linkage distance is given by:

`d(C_i, C_j) = \frac{1}{|C_i||C_j|} \sum_{a\inC_i} \sum_{b\inC_j} d(a, b)`

This criterion balances the influence of all pairs of points between clusters, often leading to clusters of moderate size and density.

4. Ward's Linkage

Ward's linkage method aims to minimize the total within-cluster variance. It defines the distance between two clusters based on the increase in the sum of squared errors (SSE) after merging the clusters. For clusters `C_i, C_j` with centroids `\bar{a}, \bar{b}` respectively, the Ward's linkage distance is given by:

`d(C_i, C_j) = \sqrt{\frac{2 |C_i| |C_j|}{|C_i| + |C_j|}}||\bar{a} - \bar{b}||`

where `||*||` denotes the Euclidean norm. This method tends to create clusters of relatively equal size and compactness.

Algorithmic Steps

The agglomerative clustering algorithm is structured around a series of systematic steps that progressively merge individual data points into larger clusters. This section outlines the detailed algorithmic steps involved in agglomerative clustering, highlighting the initialization, distance calculation, cluster merging, distance updating, and termination phases.

1. Initialization

The algorithm starts by treating each data point as a separate cluster. For a dataset containing `n` data points, the initial set of clusters can be represented as:

`C = {{x_1}, {x_2},..,{x_n}}`

where `x_i` represents the `i^{th}` data point. At this stage, there are `n` clusters, each containing a single data point.

2. Distance Calculation

The next step involves computing the distances between all pairs of clusters. The choice of distance metric (as discussed in Section 2.1) influences this calculation. The distance matrix `D` is constructed where each element `d_{ij}` ​ represents the distance between clusters `C_i` and `C_j`:

`D = {d(C_i, C_j):C_i, C_j \in C, C_i \ne C_j}`

The initial distance matrix is calculated using the chosen distance metric, providing a basis for subsequent cluster merging.

3. Merge Clusters

The algorithm proceeds by merging the two clusters that are closest to each other. This is determined by finding the pair `(C_i, C_j)` with the minimum distance in the distance matrix `D`:

`(C_i, C_j) = \argmin_{(C_i, C_j) \in C \times C} d(C_i, C_j)`

Once the closest pair of clusters is identified, they are merged to form a new cluster `C_{ij}`​. The new cluster `C_{ij}` is formed by combining all the data points from clusters `C_i` and `C_j`:

`C_{ij} = C_i \cap C_j`

4. Update Distances

After merging two clusters, the distance matrix `D` must be updated to reflect the new cluster configuration. The distances between the new cluster `C_{ij}` and all remaining clusters are recalculated using the chosen linkage criterion. The updated distance matrix is then:

`D^' = {d(C_ij, C_k): C_k\in C\\{C_i, C_j}}`

The original clusters `C_i` and `C_j` are removed from the set of clusters `C`, and the new cluster `C_{ij}` ​ is added:

`C = (C\\ {C_i, C_j}) \cup {C_{ij}}`

5. Termination

The algorithm iterates through the steps of distance calculation, cluster merging, and distance updating until all data points are merged into a single cluster. The resulting clusters can be visualized using a dendrogram, which illustrates the hierarchical structure and the order in which clusters were merged. 

Complexity and Considerations

The computational complexity of agglomerative clustering primarily depends on the distance metric and linkage criteria used. For a dataset with `n` objects: 

Time Complexity: The time complexity of agglomerative clustering is generally `O(n^3)` in the worst case. This is due to the need to compute the distance matrix and update it after each merge. However, certain optimizations and efficient data structures can reduce this complexity. 

Space Complexity: The space complexity is `O(n^2)`, as the distance matrix requires storage for all pairwise distances between clusters.

Due to its computational complexity, agglomerative clustering may not be suitable for very large datasets. In such cases, alternative clustering methods like k-means or DBSCAN may be more appropriate. Despite its computational expense, agglomerative clustering offers several advantages, including flexibility in the choice of distance metrics and linkage criteria, as well as the ability to generate a dendrogram that provides a comprehensive view of the data’s hierarchical structure.


Agglomerative clustering is a versatile and powerful tool for exploratory data analysis. Its ability to create a hierarchical structure of clusters makes it particularly useful for understanding complex datasets. While it has higher computational requirements compared to some other clustering methods, the insights gained from the hierarchical relationships can be invaluable. Understanding the mathematical underpinnings and the algorithmic implementation can help practitioners choose appropriate metrics and linkage criteria for their specific applications, leading to more meaningful and interpretable clustering results.


  1. Everitt, B., Landau, S., Leese, M., & Stahl, D. (2011). Cluster Analysis. Wiley.
  2. Kaufman, L., & Rousseeuw, P. J. (2009). Finding Groups in Data: An Introduction to Cluster Analysis. Wiley.
  3. Hastie, T., Tibshirani, R., & Friedman, J. (2009). The Elements of Statistical Learning: Data Mining, Inference, and Prediction. Springer.

Thank you for visiting my blog! I am Stefanos Stavrianos, and I have studied at premier global universities. I hold a Specialization in Quantitative Finance from the Higher School of Economics in Moscow, and a Python 3 Programming Specialization from the University of Michigan. My academic interests encompass microeconomics, macroeconomics and monetary economics, with a research focus on financial crises.

Post a Comment