{"id":279,"date":"2026-11-13T12:36:46","date_gmt":"2026-11-13T04:36:46","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-graham-scan-builds-a-convex-hull\/"},"modified":"2026-11-13T21:58:24","modified_gmt":"2026-11-13T13:58:24","slug":"how-the-graham-scan-builds-a-convex-hull","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-graham-scan-builds-a-convex-hull\/","title":{"rendered":"How the Graham scan builds a convex hull"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> The <strong>Graham scan<\/strong> computes the convex hull of a finite set of points in the plane. It anchors the lowest point, sorts the rest by polar angle around that anchor, and walks the sorted list with a stack. A right turn, or a non-left turn, pops the stack, because the middle point of a right turn cannot be on the hull. The scan is <strong>O(n log n)<\/strong>, and the sort is the expensive part. The stack walk after the sort is linear.<\/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\">The convex hull is the smallest convex polygon that contains every point. Brute force over triples is cubic. Once the points are ordered by angle around an interior-or-boundary point, the hull is a single left-turning chain. The lowest point is a hull vertex, so it is a legal anchor. Sorting by the cross product of vectors from the anchor avoids trigonometry and avoids comparing angles that differ by less than an ulp of a cosine.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Collinear points are the edge case. If the problem wants the minimum hull, with no redundant boundary points, a zero cross product is not a left turn and the middle point is popped. If collinear boundary points must be kept, the predicate changes, and the writeup has to say so.<\/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 stack at the end is the hull in counterclockwise order, starting from the anchor, under the usual left-turn rule. Time is O(n log n) from the sort, then O(n) for the walk, because each point is pushed once and popped at most once. Extra memory is the sorted array and the stack. Duplicate points should be removed, or the polar sort needs a tie rule: closer points first or farther points first, chosen so the scan does not keep an interior duplicate.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The algorithm is for the Euclidean plane. It is not a 3D hull. A point set with all points identical returns that single point. Fewer than three noncollinear points returns the degenerate hull, a segment or a point, and the loop should not assume a triangle exists.<\/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>Choose the anchor as the lowest y, breaking ties by smaller x. It is on the hull.<\/li>\n<li>Sort the other points by polar angle around the anchor, using the sign of the cross product (b\u2212a) \u00d7 (c\u2212a). Break remaining ties by distance from the anchor, keeping the farther point later so the scan can drop the nearer one.<\/li>\n<li>Push the anchor and the first sorted point. For each next point, pop while the top two points and the new point are not a left turn. Then push the new point.<\/li>\n<li>The cross product (b\u2212a) \u00d7 (c\u2212a) is positive for a left turn, zero for collinear, and negative for a right turn. Pop on nonpositive if interior collinear points must go.<\/li>\n<li>The stack, from bottom to top, is the hull in boundary order.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The log factor is the sort. Andrew&#8217;s monotone chain also sorts, by x coordinate, and builds an upper and a lower hull. It avoids polar angles and is usually the implementation to prefer. Jarvis march is the gift-wrapping algorithm, output-sensitive and quadratic in the worst case. All three compute the same polygon up to collinear-point conventions.<\/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 a right turn is left on the stack<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Push every point in angle order and never pop<\/td>\n<td>The chain can dent inward. The pop is the convexity repair.<\/td>\n<\/tr>\n<tr>\n<td>Sort by the slope as a floating-point division<\/td>\n<td>Vertical rays and near-ties mis-order the points. The cross product compares angles without dividing.<\/td>\n<\/tr>\n<tr>\n<td>Pick an arbitrary interior point as the anchor and start the stack there<\/td>\n<td>An interior anchor is not a hull vertex. The lowest point is used because it is on the hull and every other point has a defined upward angle.<\/td>\n<\/tr>\n<tr>\n<td>Keep three collinear points while claiming the minimum vertex set<\/td>\n<td>A non-strict left turn has to pop the middle point. Keeping it is a different, documented convention.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">A sort by x coordinate with two chains is Andrew&#8217;s monotone chain. Gift wrapping is Jarvis march. The Graham scan is the polar-angle stack.<\/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 sorts by polar angle around the lowest point and pops any turn that is not a left turn. On the same diamond as the monotone-chain example the hull has <code>4<\/code> vertices.<\/p>\n\n<pre><code class=\"language-python\">pts = [(0, 0), (1, 1), (2, 0), (1, -1)]\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\nimport math\ndef graham(points):\n    pivot = min(points, key=lambda p: (p[1], p[0]))\n    rest = sorted((p for p in points if p != pivot), key=lambda p: (math.atan2(p[1] - pivot[1], p[0] - pivot[0]), (p[0] - pivot[0]) ** 2 + (p[1] - pivot[1]) ** 2))\n    stack = [pivot]\n    for p in rest:\n        while len(stack) &gt;= 2 and cross(stack[-2], stack[-1], p) &lt;= 0:\n            stack.pop()\n        stack.append(p)\n    return stack\n\nassert len(graham(pts)) == 4\nprint(len(graham(pts)))\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\">Graham&#8217;s scan builds a 2D convex hull by sorting points by angle. Mapping and collision code more often use Andrew&#8217;s monotone chain, which sorts by x and avoids the polar comparison. Both return the same hull on general position.<\/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 polar sort when an implementation can sort by x and run the monotone chain. The arithmetic is simpler and the hull is the same. Keep Graham when the angular view is already the one the surrounding code uses, and define the collinear case explicitly.<\/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 the Graham scan returns the lowest-point polar sort and the stack that pops every non-left turn. <strong>The Graham scan computes a planar convex hull in O(n log n) time by sorting points around the lowest point and popping every right turn from a stack.<\/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: The Graham scan computes the convex hull of a finite set of points in the plane. It anchors the lowest point, sorts the rest by polar angle around that anchor, and walks the sorted list with a stack. A right turn, or a non-left turn, pops the&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-279","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\/279","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=279"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/279\/revisions"}],"predecessor-version":[{"id":678,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/279\/revisions\/678"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}