{"record":{"id":"2229aae422edbc2f","repo":"krahets/hello-algo","slug":"vertex-index-out-of-bounds","errorCode":null,"errorMessage":"Vertex index out of bounds\n","messagePattern":"Vertex index out of bounds\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"en/codes/c/chapter_graph/graph_adjacency_matrix.c","lineNumber":54,"sourceCode":"/* Add vertex */\nvoid addVertex(GraphAdjMat *graph, int val) {\n    if (graph->size == MAX_SIZE) {\n        fprintf(stderr, \"Graph vertex count has reached maximum\\n\");\n        return;\n    }\n    // Add nth vertex and zero nth row and column\n    int n = graph->size;\n    graph->vertices[n] = val;\n    for (int i = 0; i <= n; i++) {\n        graph->adjMat[n][i] = graph->adjMat[i][n] = 0;\n    }\n    graph->size++;\n}\n\n/* Remove vertex */\nvoid removeVertex(GraphAdjMat *graph, int index) {\n    if (index < 0 || index >= graph->size) {\n        fprintf(stderr, \"Vertex index out of bounds\\n\");\n        return;\n    }\n    // Remove the vertex at index from the vertex list\n    for (int i = index; i < graph->size - 1; i++) {\n        graph->vertices[i] = graph->vertices[i + 1];\n    }\n    // Remove the row at index from the adjacency matrix\n    for (int i = index; i < graph->size - 1; i++) {\n        for (int j = 0; j < graph->size; j++) {\n            graph->adjMat[i][j] = graph->adjMat[i + 1][j];\n        }\n    }\n    // Remove the column at index from the adjacency matrix\n    for (int i = 0; i < graph->size; i++) {\n        for (int j = index; j < graph->size - 1; j++) {\n            graph->adjMat[i][j] = graph->adjMat[i][j + 1];\n        }\n    }","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/c/chapter_graph/graph_adjacency_matrix.c#L36-L72","documentation":"Emitted by removeVertex() when the supplied index is outside [0, graph->size). Because the graph stores vertices in a contiguous array indexed by position (not by value), an out-of-range index has no meaning and is rejected. The function logs to stderr and returns void without modifying the graph, so the caller has no programmatic signal of failure.","triggerScenarios":"removeVertex(graph, index) with index < 0 or index >= graph->size. Examples: removeVertex(graph, 5) on a 5-vertex graph (valid indices are 0..4); removeVertex(graph, 3) using a stale index after an earlier deletion shifted vertices left.","commonSituations":"Caching a vertex index across mutations — removeVertex shifts every higher-indexed vertex down by one, so old indices go stale; confusing a vertex's stored value with its array position; using a loop counter as an index while deleting inside the loop.","solutions":["Guard the call: verify 0 <= index && index < graph->size before invoking removeVertex.","After each deletion, recompute any cached indices — removeVertex shifts all vertices above 'index' down by one.","If you only know the vertex value, scan graph->vertices[0..size-1] to find its current index before removing."],"exampleFix":"// before\nremoveVertex(graph, 5);   // graph->size == 5 -> out of bounds\n\n// after\nif (graph->size > 0) removeVertex(graph, graph->size - 1);","handlingStrategy":"validation","validationCode":"static inline int graphValidIndex(const GraphAdjMat *g, int idx) {\n    return g != NULL && idx >= 0 && idx < g->size;\n}\n\n/* usage */\nif (graphValidIndex(graph, index)) {\n    removeVertex(graph, index);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never cache vertex indices across mutations — re-derive them from vertices[] each time.","Name variables to distinguish values from indices (vertexVal vs vertexIdx).","Remember removeVertex shifts every vertex with index > 'index' down by one."],"tags":["graph","bounds","index","adjacency-matrix","c"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}