Algorithm to find points that are furthest apart — better than O(n^2)?

后端 未结 7 1169
半阙折子戏
半阙折子戏 2020-12-08 22:30

In my program, I have a set of points. For purposes of rescaling, I am searching for the two nodes that are furthest apart, and then computing a factor by which to multiply

7条回答
  •  感情败类
    2020-12-08 23:09

    The distance from A to B is the same as the distance from B to A. You can easily modify the algorithm to eliminate half of the computations this way. It'll still be O(n^2) but will be twice as fast.

    That is, instead of computing all off-diagonal elements of the distance matrix P x P:

    P = {A, B, C, D, ...}
    
      + A + B + C + D + ...
    A |   | * | * | * | ...
    B | * |   | * | * | ...
    C | * | * |   | * | ...
    D | * | * | * |   | ...
      |   |   |   |   |
    

    you can compute either the upper triangle:

      + A + B + C + D + ...
    A |   | * | * | * | ...
    B |   |   | * | * | ...
    C |   |   |   | * | ...
    D |   |   |   |   | ...
      |   |   |   |   |
    

    or the lower triangle:

      + A + B + C + D + ...
    A |   |   |   |   | ...
    B | * |   |   |   | ...
    C | * | * |   |   | ...
    D | * | * | * |   | ...
      |   |   |   |   |
    

提交回复
热议问题