{"id":155,"date":"2026-11-13T12:32:58","date_gmt":"2026-11-13T04:32:58","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-chans-algorithm-hulls-in-n-log-h-time\/"},"modified":"2026-11-13T21:51:27","modified_gmt":"2026-11-13T13:51:27","slug":"how-chans-algorithm-hulls-in-n-log-h-time","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-chans-algorithm-hulls-in-n-log-h-time\/","title":{"rendered":"How Chan&#8217;s algorithm hulls in n log h time"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Chan&#8217;s algorithm<\/strong> computes a planar convex hull in <strong>O(n log h)<\/strong> time, where h is the number of hull vertices. It guesses a parameter m, splits the points into groups of size m, and hulls each group with a standard O(m log m) hull. Gift wrapping then walks the outer hull, and each next edge is a tangent found by binary search on every group hull. If the wrap needs more than m steps, the guess was below h and the algorithm squares m and starts over. The successful guess costs O(n log h), and the failed guesses form a geometric series.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Andrew&#8217;s monotone chain and the Graham scan are O(n log n) regardless of how small the hull is. Jarvis&#8217;s march is O(n h), which is better only when h is tiny and worse when h is linear. The lower bound for the planar hull is \u03a9(n log h), so there is room between Jarvis and a full sort. Chan&#8217;s algorithm fills that room by using a small hull as a search structure inside a wrapping loop.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The group hulls are convex, so the tangent from an external point to a group hull is a binary search, not a scan of the group. Wrapping still takes h steps. The parameter m caps the number of steps the algorithm is willing to take before it admits that h is larger than it guessed.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The output is the same convex hull any exact algorithm returns, including the choice of which collinear points are kept, which must match the group hull and the tangent search. Time is O(n log h). Extra memory is the group hulls. The guess schedule m, m\u00b2, m\u2074, \u2026 starting from a small constant reaches a value at least h after O(log log h) rounds, and the last round dominates.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The algorithm is comparison-based and planar. A higher-dimensional hull is a different algorithm. Degenerate sets, all points identical or all collinear, are hulls with a small h and fall out of the same analysis. The speedup over O(n log n) appears when h is much smaller than n.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Start with a small guess such as m = 2. Partition the n points into groups of at most m points.<\/li>\n<li>Compute each group&#8217;s convex hull with Andrew&#8217;s monotone chain or the Graham scan.<\/li>\n<li>Start gift wrapping at the lowest point. At each step, find the supporting tangent from the current vertex to every group hull by binary search, and step to the best tangent&#8217;s touch point.<\/li>\n<li>If the wrap closes in at most m steps, return that hull. If a step would pass m, abort this guess.<\/li>\n<li>Square m and repeat. Stop at the first guess that finishes.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">O(n log h) is this output-sensitive schedule. A full sort of the points is Andrew or Graham and does not need a guess. A plain wrap that scans every point at every step is Jarvis&#8217;s march. Chan&#8217;s algorithm is the group-hull tangent wrap.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the wrap keeps going past a failed guess<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Continue wrapping after m steps on a guess that was too small<\/td>\n<td>The time bound came from stopping. Finishing the wrap at m much smaller than h recreates an expensive march and wastes the guess schedule.<\/td>\n<\/tr>\n<tr>\n<td>Scan every point of a group to find the tangent<\/td>\n<td>The group is already a convex hull. The tangent is a binary search. A linear scan removes the log factor the algorithm exists to get.<\/td>\n<\/tr>\n<tr>\n<td>Square n instead of m<\/td>\n<td>The parameter that must exceed h is m, the group size and the step cap. Squaring the whole input does not implement the schedule.<\/td>\n<\/tr>\n<tr>\n<td>Ignore collinear ties inside the tangent search<\/td>\n<td>The inner hull and the wrap can then keep different points on the same edge. The tie rule has to be the same one the hull definition uses.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">A sort-based hull is Andrew&#8217;s monotone chain or the Graham scan. A linear scan per hull edge is Jarvis&#8217;s march. Chan&#8217;s algorithm is the output-sensitive combination.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment hulls groups of size m, then gift-wraps by binary-searching a tangent to each group hull. A guess that would take more than m steps is aborted and m is squared. The diamond with an interior point has hull size <code>4<\/code>.<\/p>\n\n<pre><code class=\"language-python\">pts = [(0, 0), (2, 0), (1, 1), (1, -1), (1, 0)]\n\ndef cross(o, a, b):\n    return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])\n\ndef andrew(points):\n    points = sorted(set(points))\n    if len(points) &lt;= 1:\n        return points\n    lower = []\n    for p in points:\n        while len(lower) &gt;= 2 and cross(lower[-2], lower[-1], p) &lt;= 0:\n            lower.pop()\n        lower.append(p)\n    upper = []\n    for p in reversed(points):\n        while len(upper) &gt;= 2 and cross(upper[-2], upper[-1], p) &lt;= 0:\n            upper.pop()\n        upper.append(p)\n    return lower[:-1] + upper[:-1]\n\ndef more_ccw(origin, a, b):\n    turn = cross(origin, a, b)\n    if turn == 0:\n        return (a[0] - origin[0]) ** 2 + (a[1] - origin[1]) ** 2 &gt; (b[0] - origin[0]) ** 2 + (b[1] - origin[1]) ** 2\n    return turn &gt; 0\n\ndef tangent(hull, origin):\n    best = 0\n    step = 1\n    while step &lt; len(hull):\n        step *= 2\n    while step:\n        cand = (best + step) % len(hull)\n        alt = (best - step) % len(hull)\n        if more_ccw(origin, hull[cand], hull[best]):\n            best = cand\n        if more_ccw(origin, hull[alt], hull[best]):\n            best = alt\n        step \/\/= 2\n    return hull[best]\n\ndef chan(points):\n    m = 2\n    while True:\n        groups = [points[i:i + m] for i in range(0, len(points), m)]\n        hulls = [andrew(g) for g in groups]\n        start = min(points)\n        hull = [start]\n        for _ in range(m):\n            best = None\n            for group in hulls:\n                touch = tangent(group, hull[-1])\n                if touch == hull[-1]:\n                    continue\n                if best is None or more_ccw(hull[-1], touch, best):\n                    best = touch\n            if best == start:\n                return hull\n            hull.append(best)\n        m *= m\n\ngot = chan(pts)\nassert set(got) == set(andrew(pts)) and len(got) == 4\nprint(len(got))\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Chan&#8217;s algorithm is the output-sensitive convex hull in geometry libraries that want n log h rather than n log n. It is worth it when the hull has few corners. Andrew&#8217;s monotone chain is the simpler hull those libraries use when h is not small.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop the guess schedule when h is expected to be linear in n. Andrew&#8217;s chain is simpler and the same order. Keep Chan&#8217;s algorithm when the hull is expected to be much smaller than the set and the O(n log h) bound is the goal.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for Chan&#8217;s convex hull returns the group hulls, the tangent binary search, and the squared guesses. <strong>Chan&#8217;s algorithm computes a planar convex hull in O(n log h) time by wrapping tangents of group hulls and squaring the group size until the guess reaches h.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Chan&#8217;s algorithm computes a planar convex hull in O(n log h) time, where h is the number of hull vertices. It guesses a parameter m, splits the points into groups of size m, and hulls each group with a standard O(m log m) hull. Gift wrapping then&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,40],"tags":[],"class_list":["post-155","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-geometry"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=155"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/155\/revisions"}],"predecessor-version":[{"id":554,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/155\/revisions\/554"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}