【发布时间】:2020-02-21 05:12:42
【问题描述】:
Andrew 的单调链凸包算法在 O(n\log n) 时间内构造了一组二维点的凸包。 我按照算法的步骤,发现它具有 O(n Logn) 时间复杂度。排序只是找到最低的 X,然后在相等的情况下找到较低的 Y。我最初没有使用堆或其他排序。在哪一行,有Log操作吗? 可以从下面提供的链接中找到更多信息。
链接:https://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain
points.Sort();
if (points.Count <= 3)
{
return new List<PlaceOfInterest>(points);
}
List<PlaceOfInterest> upperHull = new List<PlaceOfInterest>();
foreach (PlaceOfInterest point in points)
{
PlaceOfInterest p2 = point;
while (upperHull.Count >= 2)
{
PlaceOfInterest pivot = upperHull[upperHull.Count - 2];
PlaceOfInterest p1 = upperHull[upperHull.Count - 1];
if (Calculation.SignedArea(pivot, p1, p2) <= 0)
{
upperHull.RemoveAt(upperHull.Count - 1);
}
else
{
break;
}
}
upperHull.Add(p2);
}
upperHull.RemoveAt(upperHull.Count - 1);
List<PlaceOfInterest> lowerHull = new List<PlaceOfInterest>();
for (int i = points.Count - 1; i >= 0; i--)
{
PlaceOfInterest p2 = points[i];
while (lowerHull.Count >= 2)
{
PlaceOfInterest pivot = lowerHull[lowerHull.Count - 2];
PlaceOfInterest p1 = lowerHull[lowerHull.Count - 1];
if (Calculation.SignedArea(pivot, p1, p2) <= 0)
{
lowerHull.RemoveAt(lowerHull.Count - 1);
}
else
{
break;
}
}
lowerHull.Add(p2);
}
lowerHull.RemoveAt(lowerHull.Count - 1);
if (!(Enumerable.SequenceEqual(upperHull, lowerHull)))
{
upperHull.AddRange(lowerHull);
}
return upperHull;
【问题讨论】:
-
O(log n)并不意味着算法包含对数。 stackoverflow.com/questions/2307283/… 的可能重复项
标签: c# algorithm convex-hull