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
- Ensure both vertices exist first: check 'i >= 0 && j >= 0 && i < graph->size && j < graph->size && i != j'.
- Add both vertices via addVertex before adding the edge between them.
- Avoid self-loops (i == j) — this implementation explicitly rejects them.
- 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
- C has no exceptions — validate both indices and i != j before calling.
- Add both vertices via addVertex before adding an edge between them.
- Self-loops (i == j) are explicitly rejected by this implementation.
- Parameters are vertex INDICES, not values — do not confuse them.
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
- 顶点索引越界
- 图的顶点数量已达最大值
- Edge index out of bounds or equal
- 辺インデックスが範囲外であるか、同一です
- индексы ребра выходят за границы или совпадают
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/b14c0a949a847b4b.
Report an issue: GitHub.