{"id":156,"date":"2026-11-13T12:33:00","date_gmt":"2026-11-13T04:33:00","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-christofides-approximates-a-metric-tour\/"},"modified":"2026-11-13T21:51:28","modified_gmt":"2026-11-13T13:51:28","slug":"how-christofides-approximates-a-metric-tour","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-christofides-approximates-a-metric-tour\/","title":{"rendered":"How Christofides approximates a metric tour"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Christofides&#8217; algorithm<\/strong> builds a traveling-salesman tour that is at most <strong>3\/2<\/strong> times optimal when the distances are symmetric and obey the triangle inequality. It computes a minimum spanning tree, finds a minimum-cost perfect matching on the vertices of odd degree, and shortcuts an Eulerian circuit of the combined multigraph. The matching has at most half the optimal tour&#8217;s cost, and the tree has at most the optimal tour&#8217;s cost, which is where 3\/2 comes from. Without the triangle inequality the shortcut step can make the tour worse, and the ratio proof stops.<\/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\">A minimum spanning tree is a relaxation of a tour: a tour minus one edge is a spanning tree, so the tree costs at most the optimal tour. The tree is not a tour. Vertices of odd degree are exactly the ones an Eulerian circuit cannot pass cleanly. There is an even number of them. A perfect matching on those vertices adds one edge per odd vertex and makes every degree even. Hierholzer&#8217;s algorithm then finds an Eulerian circuit. Shortcutting repeated vertices, using the triangle inequality, does not increase the length and produces a Hamiltonian tour.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The matching is taken in the original metric, on the odd-degree subset only. Matching all vertices, or matching in the tree metric incorrectly, breaks the factor.<\/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\">On a complete undirected graph with a metric distance, the algorithm returns a tour of cost at most 3\/2 times the optimal tour. The running time is dominated by the minimum-cost perfect matching, which is polynomial and heavier than the spanning tree and the Eulerian circuit. Extra memory is the tree, the matching, and the circuit. The ratio is tight in the worst case up to lower-order improvements from later algorithms. It is not an exact solver. Held-Karp is exact and exponential.<\/p>\n\n\n<p class=\"wp-block-paragraph\">If the distances violate the triangle inequality, shortcutting can increase the cost and the 3\/2 proof does not apply. Asymmetric distances are a different problem with different approximation results.<\/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>Reject the ratio claim if the distances are not a metric. Then compute a minimum spanning tree, by Kruskal or Prim.<\/li>\n<li>List the vertices of odd degree in that tree. Compute a minimum-cost perfect matching on the subgraph they induce.<\/li>\n<li>Add the matching edges to the tree. Every degree is even. Find an Eulerian circuit with Hierholzer&#8217;s algorithm.<\/li>\n<li>Walk the circuit and skip any vertex already visited. The triangle inequality makes each shortcut no longer than the path it replaces.<\/li>\n<li>The resulting Hamiltonian tour is the output. Its cost is at most the tree plus the matching, which is at most 3\/2 of optimal.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The 3\/2 factor is a metric worst-case guarantee, not a typical error. An exact tour on a small instance is Held-Karp. A local improvement heuristic such as 2-opt has no such factor in general. The matching is the expensive subroutine and the source of the extra 1\/2.<\/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 shortcutting is used on a non-metric distance<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Skip vertices when the direct edge is longer than the path being skipped<\/td>\n<td>The tour may still be Hamiltonian and the cost can exceed the Eulerian circuit. The 3\/2 proof used the triangle inequality to say that does not happen.<\/td>\n<\/tr>\n<tr>\n<td>Match all vertices instead of the odd-degree ones<\/td>\n<td>The degree parity fix needs one incident matching edge per odd vertex. Matching everyone adds the wrong set and can leave odd degrees.<\/td>\n<\/tr>\n<tr>\n<td>Quote 3\/2 for an asymmetric distance matrix<\/td>\n<td>The spanning tree, the matching, and the Eulerian circuit were undirected. Asymmetric TSP needs a different construction.<\/td>\n<\/tr>\n<tr>\n<td>Replace the minimum matching with a greedy pairing and keep the ratio<\/td>\n<td>The 1\/2 factor uses a minimum-cost perfect matching. A greedy pairing can be more expensive than half the optimal tour.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">An exact small tour is Held-Karp. An Eulerian circuit is Hierholzer. Christofides&#8217; algorithm is the metric 3\/2 tour from a spanning tree plus a matching.<\/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 a minimum spanning tree, adds a minimum perfect matching on the odd-degree vertices, and shortcuts an Eulerian circuit. On the unit square the tour length is <code>4<\/code>, which is within 3\/2 of optimal.<\/p>\n\n<pre><code class=\"language-python\">cities = [(0, 0), (1, 0), (1, 1), (0, 1)]\n\ndef dist(i, j):\n    dx = cities[i][0] - cities[j][0]\n    dy = cities[i][1] - cities[j][1]\n    return (dx * dx + dy * dy) ** 0.5\n\nedges = sorted((dist(i, j), i, j) for i in range(4) for j in range(i + 1, 4))\nparent = list(range(4))\n\ndef find(x):\n    while parent[x] != x:\n        parent[x] = parent[parent[x]]\n        x = parent[x]\n    return x\n\ntree = []\nfor w, i, j in edges:\n    a, b = find(i), find(j)\n    if a != b:\n        parent[a] = b\n        tree.append((i, j))\ndegree = [0] * 4\nadj = [[] for _ in range(4)]\nfor i, j in tree:\n    degree[i] += 1\n    degree[j] += 1\n    adj[i].append(j)\n    adj[j].append(i)\nodds = [i for i in range(4) if degree[i] % 2]\npair = []\nif len(odds) == 2:\n    pair = [(odds[0], odds[1])]\nelse:\n    options = [\n        [(odds[0], odds[1]), (odds[2], odds[3])],\n        [(odds[0], odds[2]), (odds[1], odds[3])],\n        [(odds[0], odds[3]), (odds[1], odds[2])],\n    ]\n    def cost(matching):\n        return sum(dist(i, j) for i, j in matching)\n    pair = min(options, key=cost)\nfor i, j in pair:\n    adj[i].append(j)\n    adj[j].append(i)\nstack, circuit = [0], []\nunused = [list(vs) for vs in adj]\nwhile stack:\n    u = stack[-1]\n    if unused[u]:\n        v = unused[u].pop()\n        unused[v].remove(u)\n        stack.append(v)\n    else:\n        circuit.append(stack.pop())\nseen, tour = set(), []\nfor v in circuit:\n    if v not in seen:\n        seen.add(v)\n        tour.append(v)\ntour.append(tour[0])\nlength = sum(dist(a, b) for a, b in zip(tour, tour[1:]))\nassert abs(length - 4) &lt; 1e-9 and len(seen) == 4\nprint(int(length))\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\">Christofides&#8217; algorithm builds a 1.5-approximate metric tour and is a starting point for vehicle-routing heuristics. A shipping solver then improves that tour with local search. Road routers do not ship the 1.5 proof as the route a driver sees.<\/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 ratio claim on non-metric distances. Keep the algorithm when the distances are a metric and a tour within 3\/2 of optimal is the goal, and spend the computation on a real minimum-cost matching.<\/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 Christofides&#8217; algorithm returns the tree, the odd-degree matching, and the 3\/2 argument. <strong>Christofides&#8217; algorithm returns a metric traveling-salesman tour of cost at most 3\/2 of optimal by shortcutting an Eulerian circuit of a minimum spanning tree plus a minimum matching on odd-degree vertices.<\/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: Christofides&#8217; algorithm builds a traveling-salesman tour that is at most 3\/2 times optimal when the distances are symmetric and obey the triangle inequality. It computes a minimum spanning tree, finds a minimum-cost perfect matching on the vertices of odd degree, and shortcuts an Eulerian circuit of 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,32],"tags":[],"class_list":["post-156","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-graphs"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/156","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=156"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/156\/revisions"}],"predecessor-version":[{"id":555,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/156\/revisions\/555"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}