TheAlgorithms/C-Sharp · error
At least two cities are required.
Error message
At least two cities are required.
What it means
This ArgumentException is the input-size guard in TravelingSalesmanSolver.SolveBruteForce (TravelingSalesmanSolver.cs:25), after the square-matrix check. The brute-force route construction fixes city 0 as start and end and permutes the remaining cities; with fewer than 2 cities there is no round trip to optimize. It fires when distanceMatrix has dimension n < 2.
Solutions
- Ensure at least two cities exist before solving; handle 0/1-city cases as trivial (empty or zero-distance route) at the call site.
- Validate matrix dimension >= 2 before calling.
- Fix upstream filtering that removed all but one city.
Example fix
// before
var r = TravelingSalesmanSolver.SolveBruteForce(m);
// after
var r = n >= 2 ? TravelingSalesmanSolver.SolveBruteForce(m) : (new[] { 0 }, 0.0); Defensive patterns
Strategy: validation
Validate before calling
if (m.GetLength(0) < 2) throw new InvalidOperationException("TSP needs at least two cities"); Try / catch
try { var r = TravelingSalesmanSolver.SolveBruteForce(m); }
catch (ArgumentException ex) { return (Array.Empty<int>(), 0.0); } Prevention
- Handle 0/1-city datasets as trivial cases upstream
- Check dataset cardinality after filtering
- Document minimum input size in calling code
When it happens
Trigger: Calling SolveBruteForce with a 0x0 or 1x1 distance matrix, e.g. a single-city dataset or an empty matrix.
Common situations: Dataset filtered down to one row; empty input file producing a 0x0 matrix; caller not guarding the trivial single-city case.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- Distance matrix must be square.
- Specified argument was out of the range of valid values…
- Invalid parameter settings for Ascon Hash
- Cash flows list cannot be empty
- cannot be negative
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/e1bb7244058bfc6e.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Problems/TravelingSalesman/TravelingSalesmanSolver.cs:25
public static class TravelingSalesmanSolver
{
/// <summary>
/// Solves the TSP using brute-force search. This method checks all possible permutations of cities to find the shortest possible route.
/// WARNING: This approach is only feasible for small numbers of cities due to factorial time complexity.
/// </summary>
/// <param name="distanceMatrix">A square matrix where element [i, j] represents the distance from city i to city j.</param>
/// <returns>A tuple containing the minimal route (as an array of city indices) and the minimal total distance.</returns>
public static (int[] Route, double Distance) SolveBruteForce(double[,] distanceMatrix)
{
int n = distanceMatrix.GetLength(0);
if (n != distanceMatrix.GetLength(1))
{
throw new ArgumentException("Distance matrix must be square.");
}
if (n < 2)
{
throw new ArgumentException("At least two cities are required.");
}
var cities = Enumerable.Range(0, n).ToArray();
double minDistance = double.MaxValue;
int[]? bestRoute = null;
foreach (var perm in Permute(cities.Skip(1).ToArray()))
{
var route = new int[n + 1];
route[0] = 0;
for (int i = 0; i < perm.Length; i++)
{
route[i + 1] = perm[i];
}
// Ensure route ends at city 0
route[n] = 0;
View on GitHub (pinned to 96e2905cab)