Distance based grouping algorithms