hierarchical_lustering(s_m)

S0 = f

initialize each node a, b, … in its own cluster ai, bi, …

Do until Si is the only cluster left

merge two clusters, say ai and bi with highest similarity d, into ni

(Si\{ai, bi}) ∪ ni//add ni to Si and delete ai and bi

return Si