krahets/hello-algo · error

Edge index out of bounds or equal

Error message

Edge index out of bounds or equal

What it means

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.

Source

Thrown at en/codes/c/chapter_graph/graph_adjacency_matrix.c:80

    for (int i = index; i < graph->size - 1; i++) {
        for (int j = 0; j < graph->size; j++) {
            graph->adjMat[i][j] = graph->adjMat[i + 1][j];
        }
    }
    // Remove the column at index from the adjacency matrix
    for (int i = 0; i < graph->size; i++) {
        for (int j = index; j < graph->size - 1; j++) {
            graph->adjMat[i][j] = graph->adjMat[i][j + 1];
        }
    }
    graph->size--;
}

/* Add edge */
// Parameters i, j correspond to the vertices element indices
void addEdge(GraphAdjMat *graph, int i, int j) {
    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {
        fprintf(stderr, "Edge index out of bounds or equal\n");
        return;
    }
    graph->adjMat[i][j] = 1;
    graph->adjMat[j][i] = 1;
}

/* Remove edge */
// Parameters i, j correspond to the vertices element indices
void removeEdge(GraphAdjMat *graph, int i, int j) {
    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {
        fprintf(stderr, "Edge index out of bounds or equal\n");
        return;
    }
    graph->adjMat[i][j] = 0;
    graph->adjMat[j][i] = 0;
}

/* Print adjacency matrix */

View on GitHub (pinned to 69932aed18)

Solutions

  1. Pass vertex INDICES (positions in vertices[]), not values — addEdge(graph, 0, 2) connects the 1st and 3rd vertices.
  2. Insert both endpoints with addVertex() first so that i and j are always < graph->size.
  3. Reject self-loops at the call site: this implementation forbids i == j by design.

Example fix

// before
addEdge(graph, 1, 3);   // 1,3 are vertex VALUES -> index 1,3 may be out of bounds

// after
int i = indexOf(graph, 1), j = indexOf(graph, 3);
if (i >= 0 && j >= 0 && i != j) addEdge(graph, i, j);
Defensive patterns

Strategy: validation

Validate before calling

static inline int graphValidEdge(const GraphAdjMat *g, int i, int j) {
    return g != NULL && i >= 0 && j >= 0
        && i < g->size && j < g->size && i != j;
}

/* usage */
if (graphValidEdge(graph, i, j)) {
    addEdge(graph, i, j);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13). Data as JSON: /api/errors/4b094dcf993afd25. Report an issue: GitHub.