【问题标题】:Why Andrew's Monotone Algorithm takes O(N LogN) times?为什么 Andrew 的单调算法需要 O(N LogN) 次?
【发布时间】: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;

【问题讨论】:

标签: c# algorithm convex-hull


【解决方案1】:

points.Sort() 需要 O(N log N) 时间。

其余的需要 O(N) 时间。 (如果你做对了——我没有检查)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 2022-10-13
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    相关资源
    最近更新 更多