K-proximity algorithm