{"id":136,"date":"2026-11-13T12:32:23","date_gmt":"2026-11-13T04:32:23","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-andrews-algorithm-builds-a-monotone-chain\/"},"modified":"2026-11-13T21:51:04","modified_gmt":"2026-11-13T13:51:04","slug":"how-andrews-algorithm-builds-a-monotone-chain","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-andrews-algorithm-builds-a-monotone-chain\/","title":{"rendered":"How Andrew&#8217;s algorithm builds a monotone chain"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Andrew&#8217;s monotone chain<\/strong> computes a planar convex hull by sorting points by increasing x, then building the lower hull and the upper hull as stacks that pop non-left turns. The sort is <strong>O(n log n)<\/strong> and each chain is linear, so the total matches the Graham scan without a polar-angle comparison. The two chains meet at the leftmost and rightmost points. Collinear points are kept or dropped by the same turn predicate the rest of the hull code uses.<\/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 Graham scan&#8217;s polar sort is the part that is easy to get wrong: ties, the anchor, and the wrap around 180 degrees. A left-to-right sort has a total order that floating-point x coordinates already approximate, with a y tie break. The lower hull, walking left to right, is a chain that only turns one way. The upper hull is the same walk with the opposite turn, or the same turn on a reversed pass.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Concatenating the chains includes the rightmost point twice if both walks push it, so one copy is dropped at the join. The leftmost point is the shared start.<\/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 concatenated chain is the convex hull in counterclockwise or clockwise order, depending on the turn convention. Time is O(n log n). Extra memory is the sorted array and two stacks. Duplicate points are removed after the sort or handled by the turn test. The algorithm assumes general position only in the sense that a robust cross product decides turns. It does not require integer coordinates, and an integer cross product avoids rounding when the coordinates themselves are integers.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The monotone chain is not faster than Graham&#8217;s asymptotically. It is simpler. Jarvis march is faster only when the hull has very few vertices.<\/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>Sort the points by increasing x, and by increasing y when x ties. Remove exact duplicates if the minimum vertex set is required.<\/li>\n<li>Build the lower hull from left to right. Push each point, and pop the middle point of the last three while those three make a right turn or a straight line that must be simplified.<\/li>\n<li>Build the upper hull from right to left, or left to right with the opposite turn test.<\/li>\n<li>Drop the first and last point of the upper hull if they duplicate the ends of the lower hull, and concatenate.<\/li>\n<li>A cross product of zero means collinear. Pop it for a minimum hull. Keep it only under an explicit convention.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The hull is planar. The sort key is x, not the polar angle. Mixing the two sorts breaks the monotone invariant and the stack can miss a hull vertex or keep an interior one. Output-sensitive wrapping is Jarvis. This algorithm always pays for the sort.<\/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 both chains keep both endpoints<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Concatenate the chains without dropping the duplicated ends<\/td>\n<td>The leftmost and rightmost points appear twice. The polygon has zero-length edges, and a later area formula can still be right while a vertex walk is wrong.<\/td>\n<\/tr>\n<tr>\n<td>Sort by polar angle and then run the two-chain pops<\/td>\n<td>The monotone invariant is an x-order invariant. Angle order is the Graham scan&#8217;s input, and the lower-upper split is no longer a left-to-right pass.<\/td>\n<\/tr>\n<tr>\n<td>Use a floating-point slope to decide the turn<\/td>\n<td>The cross product is the predicate. A slope division fails on a vertical segment and loses the sign on near-horizontal ties.<\/td>\n<\/tr>\n<tr>\n<td>Claim O(n\u00b7h) time<\/td>\n<td>The sort is O(n log n) regardless of h. The linear chains do not remove it.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Polar sorting is the Graham scan. Gift wrapping is Jarvis march. Andrew&#8217;s monotone chain is the x-sort plus two stacks.<\/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 builds the lower hull and the upper hull after sorting by x. It drops the duplicated endpoints. The hull of (0, 0), (1, 1), (2, 0), (1, -1) is the diamond. It prints the lower-left start and the vertex count, <code>0 4<\/code>.<\/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\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\nhull = andrew(pts)\nassert len(hull) == 4 and hull[0] == (0, 0)\nprint(hull[0][0], len(hull))\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\">Andrew&#8217;s monotone chain is the usual 2D convex hull in mapping and collision code: sort by x, then build the lower and upper chains. Graham&#8217;s scan computes the same hull with a polar sort. A physics engine that only needs overlap uses GJK and does not build the hull.<\/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 a polar-angle implementation when writing a hull from scratch. Sort by x and build the two chains. Keep the collinear predicate identical to the one the rest of the geometry code uses.<\/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 Andrew&#8217;s monotone chain returns the x-sort and the two stacks that join at the extreme points. <strong>Andrew&#8217;s monotone chain computes a planar convex hull in O(n log n) time by sorting on x and popping non-left turns on a lower chain and an upper chain.<\/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: Andrew&#8217;s monotone chain computes a planar convex hull by sorting points by increasing x, then building the lower hull and the upper hull as stacks that pop non-left turns. The sort is O(n log n) and each chain is linear, so the total matches the Graham scan&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-136","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\/136","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=136"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/136\/revisions"}],"predecessor-version":[{"id":535,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/136\/revisions\/535"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}