TheAlgorithms/C-Sharp · warning
No unvisited cities remain.
Error message
No unvisited cities remain.
What it means
SolveNearestNeighbor throws InvalidOperationException when no unvisited city can be found for the next hop (next == -1) before the tour covers all cities. Under normal input this is unreachable — it fires only if the matrix contains values the search cannot consume (e.g. NaN or infinity distances) so no candidate ever wins the minimum scan.
Solutions
- Sanitize the matrix: replace NaN/Infinity with large finite values or handle disconnected pairs explicitly.
- Validate all entries are finite doubles before calling.
- If the graph is genuinely disconnected, use a TSP variant that supports it instead of the plain nearest-neighbor solver.
Example fix
// before for (int i=0;i<n;i++) for (int j=0;j<n;j++) if (double.IsNaN(m[i,j])) m[i,j] = double.PositiveInfinity; solver.SolveNearestNeighbor(m); // after for (int i=0;i<n;i++) for (int j=0;j<n;j++) if (!double.IsFinite(m[i,j])) m[i,j] = 1e9; var r = solver.SolveNearestNeighbor(m);
Defensive patterns
Strategy: validation
Validate before calling
for (int i = 0; i < m.GetLength(0); i++)
for (int j = 0; j < m.GetLength(1); j++)
if (!double.IsFinite(m[i, j])) throw new ArgumentException($"Non-finite distance at [{i},{j}]"); Try / catch
try { var r = TravelingSalesmanSolver.SolveNearestNeighbor(m, start); }
catch (InvalidOperationException ex) { logger.LogError(ex, "Solver could not progress; check for NaN/Infinity distances"); } Prevention
- Replace NaN/Infinity with large finite sentinel distances
- Validate matrix finiteness after loading
- Treat disconnected graphs with an algorithm that supports them
When it happens
Trigger: A square matrix containing double.NaN or double.PositiveInfinity entries such that every unvisited city is 'worse' than the sentinel, leaving next at -1 mid-tour.
Common situations: Missing/unknown pairwise distances represented as NaN; disconnected graph encoded as infinity; matrix built by formula producing NaN (0/0, sqrt of negative).
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- Distance matrix must be square.
- At least two cities are required.
- Specified argument was out of the range of valid values…
- Detected the same node twice. Confusion how this could ever…
- Invalid parameter settings for Ascon Hash
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/36f36972a397a6e5.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Problems/TravelingSalesman/TravelingSalesmanSolver.cs:100
visited[start] = true;
double totalDistance = 0;
int current = start;
for (int step = 1; step < n; step++)
{
double minDist = double.MaxValue;
int next = -1;
for (int j = 0; j < n; j++)
{
if (!visited[j] && distanceMatrix[current, j] < minDist)
{
minDist = distanceMatrix[current, j];
next = j;
}
}
if (next == -1)
{
throw new InvalidOperationException("No unvisited cities remain.");
}
route.Add(next);
visited[next] = true;
totalDistance += minDist;
current = next;
}
totalDistance += distanceMatrix[current, start];
route.Add(start);
return (route.ToArray(), totalDistance);
}
/// <summary>
/// Generates all permutations of the input array.
/// Used for brute-force TSP solution.
/// </summary>
private static IEnumerable<int[]> Permute(int[] arr)View on GitHub (pinned to 96e2905cab)