I don't think it's a simple problem, but it is interesting.
You might try a clustering algorithms like k-means. Or perhaps there is a graph theory way to approach it.
But I suspect every solution you come up with will involve some trade offs, so there is something to be said for the simple solution you implemented.