krahets/hello-algo · warning

边索引越界或相等

Error message

边索引越界或相等

What it means

Printed to stderr by addEdge in the GraphAdjMat teaching class when either vertex index i or j is negative, >= graph->size, or when i == j (self-loops are rejected). The function prints and returns silently without modifying the adjacency matrix. Since this is an undirected graph, the check applies symmetrically to both endpoints.

Source

Thrown at 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];
        }
    }
    // 在邻接矩阵中删除索引 index 的列
    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--;
}

/* 添加边 */
// 参数 i, j 对应 vertices 元素索引
void addEdge(GraphAdjMat *graph, int i, int j) {
    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {
        fprintf(stderr, "边索引越界或相等\n");
        return;
    }
    graph->adjMat[i][j] = 1;
    graph->adjMat[j][i] = 1;
}

/* 删除边 */
// 参数 i, j 对应 vertices 元素索引
void removeEdge(GraphAdjMat *graph, int i, int j) {
    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {
        fprintf(stderr, "边索引越界或相等\n");
        return;
    }
    graph->adjMat[i][j] = 0;
    graph->adjMat[j][i] = 0;
}

/* 打印邻接矩阵 */

View on GitHub (pinned to 69932aed18)

Solutions

  1. Ensure both vertices exist first: check 'i >= 0 && j >= 0 && i < graph->size && j < graph->size && i != j'.
  2. Add both vertices via addVertex before adding the edge between them.
  3. Avoid self-loops (i == j) — this implementation explicitly rejects them.
  4. After removeVertex, recompute vertex indices since the list shifts.

Example fix

// before
addEdge(graph, 0, 5);  // fails if graph has fewer than 6 vertices

// after
if (i >= 0 && j >= 0 && i < graph->size && j < graph->size && i != j) {
    addEdge(graph, i, j);
}
Defensive patterns

Strategy: validation

Validate before calling

if (i >= 0 && j >= 0 && i < graph->size && j < graph->size && i != j) {
    addEdge(graph, i, j);
}

Type guard

/* C: precondition check before addEdge */
static inline bool valid_edge(const GraphAdjMat *g, int i, int j) {
    return i >= 0 && j >= 0 && i < g->size && j < g->size && i != j;
}

Prevention

When it happens

Trigger: Calling addEdge with a vertex index that has not been created yet (>= size), passing negative values, or attempting to add a self-loop (i == j). Also triggered if vertices were removed and stale indices are reused.

Common situations: Passing vertex values instead of indices; adding an edge before both vertices exist (vertices must be added first); attempting self-loops which this implementation forbids; using stale indices after removeVertex shifted the vertex list.

Related errors


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