{"record":{"id":"4b094dcf993afd25","repo":"krahets/hello-algo","slug":"edge-index-out-of-bounds-or-equal","errorCode":null,"errorMessage":"Edge index out of bounds or equal\n","messagePattern":"Edge index out of bounds or equal\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"en/codes/c/chapter_graph/graph_adjacency_matrix.c","lineNumber":80,"sourceCode":"    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    }\n    graph->size--;\n}\n\n/* Add edge */\n// Parameters i, j correspond to the vertices element indices\nvoid addEdge(GraphAdjMat *graph, int i, int j) {\n    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n        fprintf(stderr, \"Edge index out of bounds or equal\\n\");\n        return;\n    }\n    graph->adjMat[i][j] = 1;\n    graph->adjMat[j][i] = 1;\n}\n\n/* Remove edge */\n// Parameters i, j correspond to the vertices element indices\nvoid removeEdge(GraphAdjMat *graph, int i, int j) {\n    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n        fprintf(stderr, \"Edge index out of bounds or equal\\n\");\n        return;\n    }\n    graph->adjMat[i][j] = 0;\n    graph->adjMat[j][i] = 0;\n}\n\n/* Print adjacency matrix */","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/c/chapter_graph/graph_adjacency_matrix.c#L62-L98","documentation":"Emitted by addEdge() when either endpoint index is negative, >= graph->size, or when i == j. This implementation models a simple undirected graph (no self-loops, no multi-edges), so identical endpoints are forbidden alongside out-of-range ones. The function logs to stderr and returns void without modifying adjMat.","triggerScenarios":"addEdge(graph, i, j) where i < 0, j < 0, i >= graph->size, j >= graph->size, or i == j. Concrete cases: addEdge(graph, 0, 0) (self-loop); addEdge(graph, 5, 3) where 5 and 3 are vertex VALUES mistaken for indices; addEdge before the vertices exist.","commonSituations":"Passing vertex values instead of array indices (the API uses 0-based positions in vertices[]); attempting self-loops in a simple-graph model; creating an edge before both endpoints have been inserted via addVertex; off-by-one from a <= vs < loop bound.","solutions":["Pass vertex INDICES (positions in vertices[]), not values — addEdge(graph, 0, 2) connects the 1st and 3rd vertices.","Insert both endpoints with addVertex() first so that i and j are always < graph->size.","Reject self-loops at the call site: this implementation forbids i == j by design."],"exampleFix":"// before\naddEdge(graph, 1, 3);   // 1,3 are vertex VALUES -> index 1,3 may be out of bounds\n\n// after\nint i = indexOf(graph, 1), j = indexOf(graph, 3);\nif (i >= 0 && j >= 0 && i != j) addEdge(graph, i, j);","handlingStrategy":"validation","validationCode":"static inline int graphValidEdge(const GraphAdjMat *g, int i, int j) {\n    return g != NULL && i >= 0 && j >= 0\n        && i < g->size && j < g->size && i != j;\n}\n\n/* usage */\nif (graphValidEdge(graph, i, j)) {\n    addEdge(graph, i, j);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Name call-site variables to separate indices from values (idxA, idxB vs valA, valB).","Insert all vertices first, then add edges, so endpoints are always in range.","If you need self-loops or multi-edges, this simple-undirected model is the wrong tool — use a different structure."],"tags":["graph","bounds","edge","self-loop","adjacency-matrix","c"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}