Chose the farthest k points from given n points

后端 未结 4 1833
故里飘歌
故里飘歌 2021-02-02 17:22

I have a set S of n points in dimension d for which I can calculate all pairwise distances if need be. I need to select k points in this set so that t

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-02 17:54

    Here's a working (brute-force) implementation for small n, which if nothing else shows the expressiveness of generator comprehensions:

    selection = max(
        itertools.combinations(points, k),
        key=lambda candidate: sum(
            dist(p, q) for p, q in itertools.combinations(candidate, 2)
        )
    )
    

    Although this ends up calling dist a lot:

    (k! / 2! / (k-2)!) * (n! / k! / (n-k)! == n! /(2(k-2)!(n-k)!)
    

提交回复
热议问题