B 树修订版

发布于 2024-08-30 05:28:40 字数 332 浏览 5 评论 0原文

如果我们正在寻找线交点(仅水平和垂直线)并且我们有 n 条线,其中一半是垂直的并且没有交点,那么

使用归并排序对 y 值上的线端点列表进行排序将需要 N log N

每个插入删除和搜索我们的数据结构(假设它是一个 b 树)将是 < log n

,所以总搜索时间将为 N log N

如果使用归并排序排序的时间需要 N log N 并且插入和删除需要 < log n 的时间,那么我在这里缺少什么? log n 是我们删除常数因子以给出 N log N 的总时间。如果不是,那么怎么会 < log n 在 ONotation 总运行时间中丢失了?

谢谢

If we are looking for line intersections (horizontal and vertical lines only) and we have n lines with half of them vertical and no intersections then

Sorting the list of line end points on y value will take N log N using mergesort

Each insert delete and search of our data structue (assuming its a b-tree) will be < log n

so the total search time will be N log N

What am i missing here, if the time to sort using mergesort takes a time of N log N and insert and delete takes a time of < log n are we dropping the constant factor to give an overal time of N log N. If not then how comes < log n goes missing in total ONotation run time?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话已封尘 2024-09-06 05:28:40

大 O 表示法描述了算法的渐近行为。也就是说,它描述了算法的行为N趋向于无穷大。需要 N log N 时间的算法部分将使需要 log N 时间的算法部分相形见绌。当 N 变大时,log N 部分的重要性会降低到相对为零。

The big-O notation describes the asymptotic behavior of the algorithm. That is, it describes the behavior of the algorithm as N trends towards infinity. The portion of the algorithm that takes N log N time will dwarf the portion of the algorithm that takes log N time. The significance of the log N portion diminishes to relatively nothing as N becomes large.

划一舟意中人 2024-09-06 05:28:40

我怀疑你的导师指的是线段相交的问题,它比只需对片段进行排序即可。您会注意到,本文引用了 Shamos–Hoey 算法,该算法使用二叉树来存储线段并有效地检测相交。

迈克尔是正确的,因为使用二叉树对于线段的一次性排序是没有意义的。然而,在检测交叉点的情况下,先排序后搜索将产生二次性能,并且不是最好的方法,这就是为什么线检测算法使用二叉树。

例如,假设您沿 x 轴从左到右对线段进行排序。简单的检测算法将类似于:

for (int i=0; i<segs.length - 1; ++i) {
  boolean searching = true;

  for (int j=i+1; j<segs.length && searching; ++j) {
     if (segments overlap on x-axis) {
       // Check for intersection.
     } else {
       // No overlap so terminate inner loop early.
       // This is the advantage of having a sorted list.
       searching = false;
     }
  }
}

由于双重嵌套循环,最坏的情况是 O(n^2),并且当所有线段在 x 轴上重叠时发生。最好的情况是线性的,并且当没有任何线段在 x 轴上重叠时发生。

您可能希望首先实现朴素算法,然后再实现使用 B 树结构的算法。

I suspect that your tutor is referring to the problem of Line Segment Intersection, which is more complex than simply sorting the segments. You'll note that this article references the Shamos–Hoey algorithm, which uses a binary tree to store the line segments and efficiently detect intersections.

Michael is correct in that using a binary tree is pointless for a one-off sort of the line segments. However, in the context of detecting intersections, sorting followed by a search will yield quadratic performance and is not the best approach, hence why line detection algorithms use binary trees.

For example, suppose you sort your line segments along the x-axis from left to right. A naive detection algorithm would then be something like:

for (int i=0; i<segs.length - 1; ++i) {
  boolean searching = true;

  for (int j=i+1; j<segs.length && searching; ++j) {
     if (segments overlap on x-axis) {
       // Check for intersection.
     } else {
       // No overlap so terminate inner loop early.
       // This is the advantage of having a sorted list.
       searching = false;
     }
  }
}

Due to the doubly-nested loop the worst case is O(n^2) and occurs when all line segments overlap on the x-axis. The best case is linear and occurs when none of the segments overlap on the x-axis.

You might want to start by implementing the naive algorithm followed by one that uses a B-tree structure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文