{"record":{"id":"b3715a0fc9f28fd3","repo":"TheAlgorithms/Java","slug":"source-and-sink-must-be-valid-vertex-indices-b3715a","errorCode":null,"errorMessage":"Source and sink must be valid vertex indices","messagePattern":"Source and sink must be valid vertex indices","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/graph/PushRelabel.java","lineNumber":159,"sourceCode":"    }\n\n    private static void validate(int[][] capacity, int source, int sink) {\n        if (capacity == null || capacity.length == 0) {\n            throw new IllegalArgumentException(\"Capacity matrix must not be null or empty\");\n        }\n        int n = capacity.length;\n        for (int i = 0; i < n; i++) {\n            if (capacity[i] == null || capacity[i].length != n) {\n                throw new IllegalArgumentException(\"Capacity matrix must be square\");\n            }\n            for (int j = 0; j < n; j++) {\n                if (capacity[i][j] < 0) {\n                    throw new IllegalArgumentException(\"Capacities must be non-negative\");\n                }\n            }\n        }\n        if (source < 0 || sink < 0 || source >= n || sink >= n) {\n            throw new IllegalArgumentException(\"Source and sink must be valid vertex indices\");\n        }\n    }\n}\n","sourceCodeStart":141,"sourceCodeEnd":163,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/PushRelabel.java#L141-L163","documentation":"PushRelabel.validate throws this IllegalArgumentException when source or sink is outside [0, n). It is the final guard in validate, ensuring indices are safe before the algorithm accesses height, excess, and capacity arrays.","triggerScenarios":"Calling PushRelabel.maxFlow with source or sink negative or >= capacity.length.","commonSituations":"1-based labels passed directly. Vertex count mismatch. Sink set to numVertices instead of numVertices - 1.","solutions":["Verify source and sink are in [0, n - 1] before calling.","Convert 1-based external labels to 0-based.","Ensure the matrix dimension matches the true vertex count."],"exampleFix":"// before\nint flow = PushRelabel.maxFlow(cap, 1, numVertices);\n\n// after\nint flow = PushRelabel.maxFlow(cap, 0, numVertices - 1);","handlingStrategy":"validation","validationCode":"int n = capacity.length;\nif (source < 0 || sink < 0 || source >= n || sink >= n) {\n    throw new IllegalArgumentException(\"Invalid source/sink\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use 0-based indices.","Derive sink as n - 1.","Keep matrix dimension aligned with vertex count."],"tags":["graph-algorithm","argument-validation","index-bounds","max-flow","push-relabel"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}