# Algorithms for Finding Hamilton Circuits in Complete Graphs

Requires a Wolfram Notebook System

Interact on desktop, mobile and cloud with the free Wolfram CDF Player or other Wolfram Language products.

Requires a Wolfram Notebook System

Edit on desktop, mobile and cloud with any Wolfram Language product.

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 (March 2011)

(Wheeling Jesuit University)

Open content licensed under CC BY-NC-SA

## Snapshots

## Details

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.

## Permanent Citation