{"record":{"id":"154a727522e95db7","repo":"TheAlgorithms/Java","slug":"k-must-be-1","errorCode":null,"errorMessage":"k must be >= 1","messagePattern":"k must be >= 1","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/graph/YensKShortestPaths.java","lineNumber":132,"sourceCode":"            throw new IllegalArgumentException(\"Weights matrix must not be null or empty\");\n        }\n        int n = weights.length;\n        for (int i = 0; i < n; i++) {\n            if (weights[i] == null || weights[i].length != n) {\n                throw new IllegalArgumentException(\"Weights matrix must be square\");\n            }\n            for (int j = 0; j < n; j++) {\n                int val = weights[i][j];\n                if (val < NO_EDGE) {\n                    throw new IllegalArgumentException(\"Weights must be -1 (no edge) or >= 0\");\n                }\n            }\n        }\n        if (src < 0 || dst < 0 || src >= n || dst >= n) {\n            throw new IllegalArgumentException(\"Invalid src/dst indices\");\n        }\n        if (k < 1) {\n            throw new IllegalArgumentException(\"k must be >= 1\");\n        }\n    }\n\n    private static boolean startsWith(List<Integer> list, List<Integer> prefix) {\n        if (prefix.size() > list.size()) {\n            return false;\n        }\n        for (int i = 0; i < prefix.size(); i++) {\n            if (!Objects.equals(list.get(i), prefix.get(i))) {\n                return false;\n            }\n        }\n        return true;\n    }\n\n    private static int[][] cloneMatrix(int[][] a) {\n        int n = a.length;\n        int[][] b = new int[n][n];","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/YensKShortestPaths.java#L114-L150","documentation":"YensKShortestPaths.kShortestPaths requires k >= 1 because it always computes at least the single shortest path first, then iterates k-1 more times for additional paths. k <= 0 makes no semantic sense (you must return at least one path) and would skip the initial Dijkstra call.","triggerScenarios":"Calling with k=0, k negative, or a k value derived from user input that was not validated (e.g. a 'top-N paths' request where N=0).","commonSituations":"User-facing 'show top K results' feature where K defaults to 0 when unset, a CLI flag parsed as 0, or passing a list size as k when the list is empty.","solutions":["Clamp k to at least 1 before calling: k = Math.max(1, requestedK).","Validate upstream: if the user requested 0 paths, return an empty list without invoking the algorithm.","Default k to 1 when the parameter is optional/unset."],"exampleFix":"// before\nint k = request.getCount(); // may be 0\nList<List<Integer>> paths = YensKShortestPaths.kShortestPaths(w, s, d, k);\n\n// after\nint k = Math.max(1, request.getCount());\nList<List<Integer>> paths = YensKShortestPaths.kShortestPaths(w, s, d, k);","handlingStrategy":"validation","validationCode":"int safeK = Math.max(1, k);\nYensKShortestPaths.kShortestPaths(weights, src, dst, safeK);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default k to 1 when it is optional or user-configurable.","Validate user-supplied k at the input layer before it reaches the algorithm.","Document that k represents 'at least 1 path'."],"tags":["graph","validation","argument-range","yens","k-shortest-paths"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}