learngraphtheory.org

Guest User

Using app without sign in

Cover of the book Graph Theory Through Problems

Practice book

Graph Theory Through Problems

300 Exercises with Hints, Worked Algorithms and Complete Solutions

By Mohammed Islam Hadjoudj

Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.

Available inKindlePaperback

13 chapters

Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on Amazon
Algorithm Selection

Select an algorithm and generate steps to begin visualization

Christofides Algorithm

A tour at most 1.5 times the shortest

Travelling salesman tour at most 1.5 times the shortest: spanning tree, matching of odd vertices, Euler circuit, shortcuts

Time: O(V³)
Space: O(V²)
Use Case: Vehicle routing, circuit board drilling, starting tours for local search
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Christofides

The Christofides algorithm finds a travelling salesman tour that is guaranteed to be at most 1.5 times as long as the shortest possible tour, whenever the distances obey the triangle inequality. Nicos Christofides published it in 1976, and Anatoliy Serdyukov found it independently. For more than forty years no polynomial-time algorithm had a better guarantee for the metric TSP.

How it works

Start with a minimum spanning tree: removing one edge from the best tour leaves a spanning tree, so the tree is cheaper than the best tour. The vertices of odd degree in the tree are then paired by a minimum-weight perfect matching, which costs at most half the best tour. Tree plus matching has only even degrees, so it has an Euler circuit, of weight at most 1.5 times the optimum. Finally the circuit is shortcut: vertices already visited are skipped, which by the triangle inequality never makes the walk longer. When the graph is not complete, distances are shortest-path distances. With Edmonds' blossom algorithm for the matching it runs in O(V³) time.

Applications

Christofides is used where an exact TSP solution is too slow: vehicle routing, drilling holes in circuit boards, ordering warehouse picks and planning delivery rounds, often as a starting tour that local search such as 2-opt then improves. In algorithms courses it is the classic approximation algorithm with a proven guarantee, and it shows how spanning trees, matchings and Euler circuits fit together.

Pseudocode

Christofides builds a travelling salesman tour from a minimum spanning tree, repairs the odd degrees with a cheapest perfect matching, walks an Euler circuit and shortcuts repeated cities. On metric distances the tour is never more than 1.5 times the optimum.

Christofides(cities, d):        // d obeys the triangle inequality
    T = minimum spanning tree of the complete graph
    O = the vertices of odd degree in T
    M = minimum weight perfect matching on O
    H = T + M                     // every degree is now even
    C = an Euler circuit of H
    tour = C with every repeated city skipped
    return tour

The guarantee comes from two lower bounds on the optimal tour OPT. Deleting one edge of OPT leaves a spanning tree, so the MST weighs at most OPT. Shortcutting OPT to the odd vertices gives a cycle of length at most OPT through them, which splits into two perfect matchings, so the cheaper one, and hence M, weighs at most OPT / 2. The circuit weighs at most 1.5 OPT, and by the triangle inequality skipping cities can only shorten it.

Worked example, step by step

Run Christofides from A on the example this page loads: seven cities, every pair connected, with distances taken from the drawing.

Example graph: A complete graph on A to G. The distances used below are A-B 12, A-F 13, B-G 10, C-D 11, C-G 11, D-E 11, E-G 10, F-G 12, B-E 20, B-C 12 and E-F 13. G sits in the middle of the map, with the other six cities around it.

  1. Spanning tree. The MST is A-B, B-G, E-G, C-G, D-E and F-G, with total weight 66. Since every tour contains a spanning tree plus one edge, no tour can be shorter than 66.
  2. Odd vertices. In the tree G has degree 4, B and E have degree 2, and A, C, D and F are leaves with degree 1. Those four odd vertices must be paired.
  3. Perfect matching. Pairing A-F with C-D costs 13 + 11 = 24. The alternatives, A-C with D-F at 45 and A-D with C-F at 51, are far worse. Adding the matching to the tree gives a multigraph of weight 90 in which every degree is even.
  4. Euler circuit. One Euler circuit of tree plus matching is A-F-G-C-D-E-G-B-A. It uses each of the eight edges once and passes through G twice.
  5. Shortcut. The second visit to G is skipped, so E is joined directly to B. That edge costs 20, exactly E-G plus G-B, so here the shortcut saves nothing. The tour is A-F-G-C-D-E-B-A.

Christofides returns a tour of length 90. The optimal tour on these distances is A-B-C-D-E-G-F-A with length 81, found by checking all 360 tours, so the approximation is 11 percent above optimal, well inside the guaranteed factor of 1.5. A 2-opt pass on the Christofides tour would be the natural next step in practice, since it removes crossings that the construction leaves behind.

Complexity, and where it comes from

Time: O(n^3) · Space: O(n^2)

On a complete graph of n cities the minimum spanning tree costs O(n^2) with the array version of Prim. The minimum weight perfect matching on the odd vertices, at most n of them, costs O(n^3) with the Edmonds blossom algorithm and dominates the running time. The Euler circuit and the shortcut pass are linear in the number of edges of H, which is O(n). The distance matrix itself takes O(n^2) memory. In practice the matching is the bottleneck, and large implementations replace it by a greedy matching, which is faster but no longer carries the 1.5 guarantee.

When to use Christofides, and when not to

Christofides is the classical approximation with a proven bound. Other methods are faster, more accurate in practice, or exact on small inputs.

AlternativePrefer it whenCost
Nearest neighbourYou need a tour instantly. It has no constant factor guarantee and is typically about 25 percent above optimal.O(n^2)
Double treeA simpler bounded method is enough. Doubling the MST instead of matching gives a factor of 2.O(n^2)
2-opt and Lin-KernighanTour quality matters more than a worst-case proof. Local search usually lands within a few percent of optimal.O(n^2) per pass
Held-Karp dynamic programmingThe instance has at most about 20 cities and you want the exact optimum.O(n^2 2^n)
Branch and cut (Concorde)You need proven optimal tours on instances with thousands of cities.exponential worst case

Common pitfalls

  • Using it on non-metric distances. The 1.5 bound needs the triangle inequality. With travel times that violate it, for example because of one-way streets or tolls, shortcuts can make the tour longer and the guarantee disappears.
  • Replacing the matching by a greedy one. Greedy matching is much easier to code but can cost far more than the optimum matching, which voids the proof. If you need the guarantee, use a true minimum weight perfect matching.
  • Stopping at the constructed tour. Christofides tours often contain crossing edges that a single 2-opt move would remove. Treat the result as a starting tour for local search rather than as a final answer.
  • Expecting one unique tour. Ties in the spanning tree, the matching or the Euler circuit lead to different tours of different lengths. All of them satisfy the bound, but tests should check the length, not the exact order of cities.

Frequently asked questions

What is the Christofides algorithm?
It is an approximation algorithm for the metric travelling salesman problem, published by Nicos Christofides in 1976 and found independently by Anatoliy Serdyukov. It combines a minimum spanning tree, a minimum weight perfect matching on the odd-degree vertices and an Euler circuit to produce a tour at most 1.5 times the optimal length.
Why is the Christofides algorithm a 1.5 approximation?
The minimum spanning tree weighs at most the optimal tour, and the matching weighs at most half of it, because the optimal tour shortcut to the odd vertices splits into two perfect matchings. The Euler circuit therefore weighs at most 1.5 times optimal, and shortcutting repeated vertices cannot lengthen it when distances are metric.
What is the time complexity of Christofides?
O(n^3), dominated by the minimum weight perfect matching. The spanning tree, Euler circuit and shortcutting steps are all O(n^2) or faster.
Is there a better approximation than Christofides?
For general metric TSP, 1.5 stood as the best proven factor for over four decades. In 2020 Karlin, Klein and Oveis Gharan gave a randomised algorithm with factor 1.5 minus about 10^-36, a theoretical improvement only. For Euclidean distances, Arora and Mitchell gave approximation schemes that get arbitrarily close to optimal.
Does Christofides work on asymmetric distances?
No. It relies on an undirected spanning tree and matching, which need symmetric distances. The asymmetric TSP needs different methods, and constant factor approximations for it were only found in 2018.

Related algorithms: Traveling Salesman Problem, Prim's MST Algorithm, Chinese Postman, Hamiltonian Path

Interactive Controls

Zoom Controls

44%
Nodes: 7
Edges: 21