AHC Clustering Algorithm Demystified – DEV Neighborhood

AHC stands for Agglomerative Hierarchical Clustering, which is a hierarchical clustering algorithm that creates a tree of clusters by merging smaller clusters into bigger ones. The algorithm doesn’t require specifying the variety of clusters upfront, in contrast to k-means or GMM. As a substitute, it permits selecting any variety of clusters by reducing the tree on the proper stage.


Step 1: Deal with every knowledge level as a singleton cluster and calculate the gap matrix between all pairs of clusters.
Step 2: Discover the pair of clusters with the smallest distance and merge them into a brand new cluster. Replace the gap matrix by eradicating the rows and columns of the merged clusters and including a row and column for the brand new cluster.
Step 3: Repeat step 2 till there is just one cluster left, which is the basis of the tree.


# Enter: knowledge factors X, distance measure D
# Output: cluster tree T

# Step 1: Initialize n singleton clusters and n X n distance matrix
C = singleton_clusters(X)
M = distance_matrix(C, D)

# Initialize cluster tree
T = empty_tree()

# Loop till there is just one cluster left
whereas C.dimension > 1:

    # Step 2: Discover the pair of clusters with the smallest distance and merge them
    i, j = argmin(M)
    new_cluster = merge(C[i], C[j])

    # Replace the gap matrix
    M = update_matrix(M, i, j, new_cluster, D)

    # Replace the cluster record
    C = update_list(C, i, j, new_cluster)

    # Step 3: Add the merged cluster to the tree
    T = add_node(T, new_cluster)

# Return the ultimate cluster tree
return T
Enter fullscreen mode

Exit fullscreen mode


  • It may seize the hierarchy and construction of the information, in contrast to k-means or GMM which produce flat clusters.
  • It may deal with clusters with completely different styles and sizes, in contrast to k-means which assumes spherical clusters.
  • It may select any variety of clusters by reducing the tree on the proper stage, in contrast to k-means or GMM which want ok as an enter.


  • It’s computationally costly, because it requires calculating and updating a big distance matrix at every iteration.
  • It’s delicate to noise and outliers, as they’ll have an effect on the gap measure and the merging course of.
  • It’s troublesome to deal with completely different cluster densities, as it could merge clusters too early or too late relying on the gap measure.

References and Additional Studying

This will probably be a multipart sequence which is able to observe up with extra clustering algorithms with their working, pseudocode, benefits and downsides.

Please keep tuned for extra such content material.

When you appreciated this publish, please share it with your folks and fellow builders. And don’t neglect to observe us for extra programming tutorials and examples! 😊

And in addition,
take a look👀 @ my Portfolio
code👨‍💻 collectively @ Github
join🔗 @ LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *