Agglomerative clustering implementation