{"record":{"id":"e1bb7244058bfc6e","repo":"TheAlgorithms/C-Sharp","slug":"at-least-two-cities-are-required","errorCode":null,"errorMessage":"At least two cities are required.","messagePattern":"At least two cities are required\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Algorithms/Problems/TravelingSalesman/TravelingSalesmanSolver.cs","lineNumber":25,"sourceCode":"public static class TravelingSalesmanSolver\n{\n    /// <summary>\n    /// Solves the TSP using brute-force search. This method checks all possible permutations of cities to find the shortest possible route.\n    /// WARNING: This approach is only feasible for small numbers of cities due to factorial time complexity.\n    /// </summary>\n    /// <param name=\"distanceMatrix\">A square matrix where element [i, j] represents the distance from city i to city j.</param>\n    /// <returns>A tuple containing the minimal route (as an array of city indices) and the minimal total distance.</returns>\n    public static (int[] Route, double Distance) SolveBruteForce(double[,] distanceMatrix)\n    {\n        int n = distanceMatrix.GetLength(0);\n        if (n != distanceMatrix.GetLength(1))\n        {\n            throw new ArgumentException(\"Distance matrix must be square.\");\n        }\n\n        if (n < 2)\n        {\n            throw new ArgumentException(\"At least two cities are required.\");\n        }\n\n        var cities = Enumerable.Range(0, n).ToArray();\n        double minDistance = double.MaxValue;\n        int[]? bestRoute = null;\n\n        foreach (var perm in Permute(cities.Skip(1).ToArray()))\n        {\n            var route = new int[n + 1];\n            route[0] = 0;\n            for (int i = 0; i < perm.Length; i++)\n            {\n                route[i + 1] = perm[i];\n            }\n\n            // Ensure route ends at city 0\n            route[n] = 0;\n","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Problems/TravelingSalesman/TravelingSalesmanSolver.cs#L7-L43","documentation":"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.","triggerScenarios":"Calling SolveBruteForce with a 0x0 or 1x1 distance matrix, e.g. a single-city dataset or an empty matrix.","commonSituations":"Dataset filtered down to one row; empty input file producing a 0x0 matrix; caller not guarding the trivial single-city case.","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."],"exampleFix":"// before\nvar r = TravelingSalesmanSolver.SolveBruteForce(m);\n// after\nvar r = n >= 2 ? TravelingSalesmanSolver.SolveBruteForce(m) : (new[] { 0 }, 0.0);","handlingStrategy":"validation","validationCode":"if (m.GetLength(0) < 2) throw new InvalidOperationException(\"TSP needs at least two cities\");","typeGuard":null,"tryCatchPattern":"try { var r = TravelingSalesmanSolver.SolveBruteForce(m); }\ncatch (ArgumentException ex) { return (Array.Empty<int>(), 0.0); }","preventionTips":["Handle 0/1-city datasets as trivial cases upstream","Check dataset cardinality after filtering","Document minimum input size in calling code"],"tags":["argument-validation","csharp","tsp"],"backgroundTag":"value-out-of-range","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}