Guest
Sign in to keep your graphs and progress.

Practice book
Graph Theory Through Problems300 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.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
Pheromone finds short tours
Ants build tours at random, favouring short hops and strong pheromone, and every tour lays pheromone in proportion to how short it is
Select an algorithm and generate steps to begin visualization
Ant colony optimization (ACO) is a metaheuristic inspired by the way real ants find short paths between their nest and food. Ants lay pheromone as they walk and other ants prefer stronger trails, so the shortest path, which is walked fastest and most often, ends up with the most pheromone. Marco Dorigo turned this into an algorithm in his 1992 PhD thesis, and the Ant System of Dorigo, Maniezzo and Colorni (1996) applied it to the travelling salesman problem: find the shortest tour that visits every city once. ACO is a classic of swarm intelligence, taught in AI and operations research courses next to genetic algorithms and simulated annealing.
In each iteration every ant builds a complete tour. From city i it moves to an unvisited city j at random, with probability proportional to τ(i,j)^α × (1/d(i,j))^β: the pheromone τ on the pair, and the closeness of the city, here with α = 1 and β = 2. When all ants are done, every trail evaporates by the factor ρ = 0.5 and each ant adds 1/L to the pairs of its tour, L its length, so short tours deposit more. Over the iterations the pheromone gathers on the edges of good tours and the ants follow them more often, while evaporation lets the colony forget poor choices. The visualizer sends one ant from each city for 20 iterations, with a fixed random seed, and draws the pheromone as green lines whose width shows its strength.
ACO solves routing problems: vehicle routing with time windows, packet routing in communication networks (AntNet), and the planning of trucks, buses and deliveries. It also handles assignment and scheduling problems such as quadratic assignment, job shop scheduling and timetabling, and has been applied to protein folding and to feature selection in machine learning. Its strengths are that it adapts to changing data, since the trails simply shift, and that it combines well with local search such as 2-opt. Like other metaheuristics it finds good tours fast but proves nothing about optimality, which is why it is checked against exact methods on small instances, as this visualizer does.
The Ant System repeats two phases: every ant builds a tour with random choices biased by pheromone and distance, then the pheromone evaporates and every tour deposits on its edges.
AntSystem(cities, α = 1, β = 2, ρ = 0.5, iterations):
τ[i][j] = n / L_nn for every pair // L_nn: nearest-neighbour tour
best = none
repeat iterations times:
for each ant k = 1..n:
tour = [city k]
while some city is unvisited:
i = last city of tour
choose an unvisited j with probability
∝ τ[i][j]^α × (1 / d[i][j])^β
append j to tour
keep tour as best if it is shorter
τ = (1 - ρ) × τ // evaporation
for each ant: add 1 / L to τ on the edges of its tour
return bestα weighs the pheromone and β the distance. With α = 0 the ants ignore the trails and act as a randomized nearest-neighbour heuristic. With β = 0 they follow pheromone alone and soon lock onto a poor tour. Ant Colony System and the MAX-MIN Ant System, stronger later variants, deposit only on the best tour and bound τ to avoid that stagnation.
Load the example: nine cities with distances taken from the drawing, every pair connected.
Example graph: The cities A to I lie roughly on a loop, with H and I inside it. The nearest-neighbour tour from A has length 109, and the shortest tour, checked by dynamic programming, has length 95.
The colony found the optimal tour of length 95, 13% shorter than the nearest-neighbour tour, in iteration 11. On nine cities an exact method is still instant. ACO earns its keep on instances with hundreds or thousands of cities, where exact methods are out of reach, usually combined with local search such as 2-opt.
Time: O(k · n³) · Space: O(n²)
Building one tour takes n choices, each over up to n candidate cities, so O(n²). With n ants that makes O(n³) per iteration, and the pheromone update adds O(n²). Over k iterations the total is O(k · n³), here 20 × 9³, about 15,000 basic steps. The pheromone and distance matrices take O(n²) memory. Real implementations restrict each choice to a candidate list of the 10 to 20 nearest cities, which brings a tour down to about O(n) and makes thousands of cities practical.
ACO is one of several ways to attack the travelling salesman problem. Exact methods prove optimality, heuristics trade that for speed.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Exact dynamic programming (Held-Karp) | Up to about 20 cities, when the optimum must be proven. | O(n² · 2ⁿ) |
| Christofides | A guarantee: a tour at most 1.5 times the optimum when distances obey the triangle inequality. | O(n³) |
| 2-opt and Lin-Kernighan | Fast and very good tours in practice, often as the finishing step of other methods. | O(n²) per pass |
| Ant colony optimization | Large or changing instances and routing variants with extra constraints, combined with 2-opt. | O(k · n³), less with candidate lists |
Related algorithms: Traveling Salesman Problem, Christofides, Hamiltonian Path, Fleet Dispatching (mTSP)