Algorithms for Finding Hamilton Circuits in Complete Graphs

This Demonstration illustrates two simple algorithms for finding Hamilton circuits of "small" weight in a complete graph (i.e. reasonable approximate solutions of the traveling salesman problem): the cheapest link algorithm and the nearest neighbor algorithm. As the edges are selected, they are displayed in the order of selection with a running tally of the weights. An optimal solution can be displayed.
  • Contributed by: Marc Brodie
  • (Wheeling Jesuit University)


  • [Snapshot]
  • [Snapshot]
  • [Snapshot]
  • [Snapshot]
  • [Snapshot]
  • [Snapshot]


Both the nearest neighbor and the cheapest link are examples of "greedy" algorithms.
The nearest neighbor algorithm starts at a given vertex and at each step visits the unvisited vertex "nearest" to the current vertex by traversing an edge of minimal weight. Once all vertices have been visited, the circuit is completed by returning to the starting vertex.
The cheapest link algorithm chooses at each step an edge of minimal weight, provided the selected edge neither results in more than two incidences at any vertex nor completes a circuit that does not contain all vertices.
While the nearest neighbor algorithm results in a path at any given stage, the edges selected using the cheapest link need not be adjacent (see Snapshot 6). Thus, a traveling salesman would need to plan his entire trip ahead of time if he wanted to use the cheapest link, but could wake up each morning and decide where to go next if he were to use the nearest neighbor.
Weights for the edges are generated at random, but a fixed set of weights is included to have a repeatable example. The thumbnail and Snapshots 1–3 show that the cheapest link and nearest neighbor algorithms may or may not result in the same circuit, that different starting vertices for the nearest neighbor may result in different circuits, and that neither algorithm is optimal. Snapshots 4 and 5 show that each algorithm sometimes finds an optimal solution.
    • Share:

Embed Interactive Demonstration New!

Just copy and paste this snippet of JavaScript code into your website or blog to put the live Demonstration on your site. More details »

Files require Wolfram CDF Player or Mathematica.

Mathematica »
The #1 tool for creating Demonstrations
and anything technical.
Wolfram|Alpha »
Explore anything with the first
computational knowledge engine.
MathWorld »
The web's most extensive
mathematics resource.
Course Assistant Apps »
An app for every course—
right in the palm of your hand.
Wolfram Blog »
Read our views on math,
science, and technology.
Computable Document Format »
The format that makes Demonstrations
(and any information) easy to share and
interact with.
STEM Initiative »
Programs & resources for
educators, schools & students.
Computerbasedmath.org »
Join the initiative for modernizing
math education.
Step-by-Step Solutions »
Walk through homework problems one step at a time, with hints to help along the way.
Wolfram Problem Generator »
Unlimited random practice problems and answers with built-in step-by-step solutions. Practice online or make a printable study sheet.
Wolfram Language »
Knowledge-based programming for everyone.
Powered by Wolfram Mathematica © 2018 Wolfram Demonstrations Project & Contributors  |  Terms of Use  |  Privacy Policy  |  RSS Give us your feedback
Note: To run this Demonstration you need Mathematica 7+ or the free Mathematica Player 7EX
Download or upgrade to Mathematica Player 7EX
I already have Mathematica Player or Mathematica 7+