【问题标题】:How to find independent points in a unit square in O(n log n)?如何在 O(n log n) 的单位正方形中找到独立点?
【发布时间】:2015-04-23 04:41:27
【问题描述】:

考虑一个包含 n 个二维点的单位正方形。我们说两个点 p 和 q 在一个正方形中是独立的,如果它们之间的欧几里德距离大于 1。一个单位正方形最多可以包含 3 个相互独立的点。我想在 O(n log n) 的给定单位正方形中找到这 3 个相互独立的点。是否可以?请帮帮我。

不使用四叉树、kd-tree等任何空间数据结构,这个问题能在O(n^2)内解决吗?

【问题讨论】:

  • 将集合分成四个子集,其中每个子集包含给定象限中的所有点,然后独立点必须在不同的子集中。我不知道这是否倾向于 O(n log n) 但“log n”确实表明需要某种空间划分。

标签: algorithm independent-set


【解决方案1】:

使用Quadtree 等空间数据结构来存储您的点。四叉树中的每个节点都有一个边界框和一组 4 个子节点,以及一个点列表(叶节点除外)。这些点存储在叶子节点中。

点四叉树是对用于表示二维点数据的二叉树的改编。它共享所有四叉树的特征,但它是一棵真正的树,因为细分的中心总是在一个点上。树的形状取决于处理数据的顺序。它通常在比较二维有序数据点时非常有效,通常在 O(log n) 时间内运行。

对于每个点,维护一组独立于该点的所有点。

将所有点插入四叉树,然后遍历这些点并使用四叉树找到独立于每个点的点:

main()
{
     for each point p
         insert p into quadtree
         set p's set to empty

     for each point p
         findIndependentPoints(p, root node of quadtree)
}

findIndependentPoints(Point p, QuadTreeNode n)
{
     Point f = farthest corner of bounding box of n
     if distance between f and p < 1
         return  // none of the points in this node or
                 // its children are independent of p

     for each point q in n
         if distance between p and q > 1
             find intersection r of q's set and p's set
             if r is non-empty then
                 p, q, r are the 3 points -> ***SOLVED***
             add p to q's set of independent points
             add q to p's set of independent points

     for each subnode m of n (up 4 of them)
         findIndependentPoints(p, m)
}

你可以加快速度:

find intersection r of q's set and p's set

通过将每个集合存储为四叉树。然后你可以通过在 q 的四叉树中搜索一个独立于 p 的点来找到交集,方法是使用相同的 early-out 技术:

// find intersection r of q's set and p's set:
//     r = findMututallyIndependentPoint(p, q's quadtree root)

Point findMututallyIndependentPoint(Point p, QuadTreeNode n)
{
     Point f = farthest corner of bounding box of n
     if distance between f and p < 1
         return  // none of the points in this node or
                 // its children are independent of p

     for each point r in n
         if distance between p and r > 1
             return r

     for each subnode m of n (up 4 of them)
         findMututallyIndependentPoint(p, m)
}

使用四叉树的替代方法是使用K-d trees,它可以生成更平衡的树,其中每个叶节点距根的深度相似。在这种情况下,寻找独立点的算法是相同的,只是数据结构中的每个节点最多只能有 2 个而不是 4 个子节点,并且每个级别的边界框的大小都是可变的。

【讨论】:

  • 非常感谢您的帮助。我正在阅读四叉树,因为它对我来说是一个非常新的概念。如果我有任何疑问,我会发布。再次感谢您的帮助。
  • 这种方法平均来说是有效的,但最坏的情况可能远远超过 O(n log n)。例如,如果一个点在左下角,而所有其他点都聚集在右上象限。四叉树插入和查询的运行时间对输出高度敏感。
  • @phari。它的最坏情况复杂度是多少?会是 O(n poly-logarithmic ) 吗?
  • @samgak。最坏情况的时间复杂度是多少?我无法弄清楚。
  • @user2311963,最坏情况下的复杂度是四叉树高度的函数,这取决于存储在其中的实际坐标。更具体地说,这取决于它们之间的距离。您可以通过使点任意靠近在一起来使四叉树高度任意大。见cs.umd.edu/class/spring2008/cmsc420/L17-18.QuadTrees.pdf
【解决方案2】:

你可能想试试这个。

Pick the top left point (Y) with coordinate (0,1). Calculate distance from each point from the List to point Y.
Sort the result in increasing order into SortedPointList (L)
If the first point (A) and the last point (B) in list L are independent:
    Foreach point P in list L:
        if P is independent to both A and B:
             Return A, B, P
Pick the top right point (X) with coordinate (1,1). Calculate distance from each point from the List to point X.
Sort the result in increasing order into SortedPointList (S)
If the first point (C) and the last point (D) in list L are independent:
    Foreach point O in list S:
        if P is independent to both C and D:
             Return C, D, O
Return null

【讨论】:

  • @user2311963 你介意分享你的实现吗?
  • @user2311963 抱歉,更新了伪代码。不是很清楚。
  • 为什么返回null?你的证据是什么?如果您的算法找不到另一个独立三角形怎么办?
  • @Toby D. 找到最远的两个点后,如果两个最远点之间的距离大于 1,我们会寻找第三个。但是,剩余的 n-2 个点与最远的两个点的距离可能小于 1。在这种情况下,您的算法返回 null。现在,考虑任何角落的一点。可以在非相邻边的内部有两个相互独立的点到所选角点的距离小于最远两点的距离。
【解决方案3】:

这是一个错误的解决方案。只为 cmets 保留。如果有人找到基于最小封闭圆的另一种解决方案,请在评论中添加链接。


  1. 解决Smallest-circle problem

  2. 如果圆的直径

  3. 如果圆由 3 个点确定,请检查哪些是“相互独立的”。如果只有两个,尝试通过迭代找到第三个。

  4. 如果圆由 2 个点确定,则它们是“相互独立的”。尝试通过迭代找到第三个。

Smallest-sircle问题可以在O(N)内解决,因此整个问题的复杂度也是O(N)。

【讨论】:

  • 第 4 步有问题。可能存在相互独立的大小为 3 的集合,而最小的封闭圆的 2 个点不属于任何集合。
  • @ThomasB。好的,空闲时间我会尝试找到这种情况的示例(如果您已经有,请发布点坐标)。到目前为止,我未能找到一个。
  • 检查imgur.com/ZVuIo9a 5 分。 D 和 B 位于单位正方形的对角,定义了最小封闭圆盘。 E,F,G 构成一个长度为 1 的等边三角形,因此相互独立。但 E,F,G 中没有一个与 D 和 B 相互独立。
  • @ThomasB。在您的图片中,B、E、F 显然形成了另一个相互独立的三元组。这与问题的先决条件相矛盾。
  • 你的意思是“一个单位正方形最多可以包含3个相互独立的点。”?我以这样的方式理解它,只有大小(3)是有限的。所以不存在有 4 个独立点的集合。但是可以有任意多的 3 组。
猜你喜欢
  • 2011-12-12
  • 1970-01-01
  • 1970-01-01
  • 2011-12-11
  • 2011-11-29
  • 1970-01-01
  • 1970-01-01
  • 2014-01-21
  • 1970-01-01
相关资源
最近更新 更多