krahets/hello-algo · error
error
Error message
error
What it means
Panic thrown by addEdge(vet1, vet2) on the undirected adjacency-list graph when the operation is illegal. The guard aborts if either endpoint vertex is not present in the adjacency map, or if both arguments are the same vertex (self-loops are disallowed). This keeps the graph representation consistent: an edge can only connect two distinct, pre-registered vertices.
Source
Thrown at ru/codes/go/chapter_graph/graph_adjacency_list.go:45
for _, edge := range edges {
g.addVertex(edge[0])
g.addVertex(edge[1])
g.addEdge(edge[0], edge[1])
}
return g
}
/* Получить число вершин */
func (g *graphAdjList) size() int {
return len(g.adjList)
}
/* Добавление ребра */
func (g *graphAdjList) addEdge(vet1 Vertex, vet2 Vertex) {
_, ok1 := g.adjList[vet1]
_, ok2 := g.adjList[vet2]
if !ok1 || !ok2 || vet1 == vet2 {
panic("error")
}
// Добавить ребро vet1 - vet2, добавив анонимную struct{}
g.adjList[vet1] = append(g.adjList[vet1], vet2)
g.adjList[vet2] = append(g.adjList[vet2], vet1)
}
/* Удаление ребра */
func (g *graphAdjList) removeEdge(vet1 Vertex, vet2 Vertex) {
_, ok1 := g.adjList[vet1]
_, ok2 := g.adjList[vet2]
if !ok1 || !ok2 || vet1 == vet2 {
panic("error")
}
// Удалить ребро vet1 - vet2
g.adjList[vet1] = DeleteSliceElms(g.adjList[vet1], vet2)
g.adjList[vet2] = DeleteSliceElms(g.adjList[vet2], vet1)
}
View on GitHub (pinned to 69932aed18)
Solutions
- Call addVertex() for both endpoints before addEdge().
- Guard the call: confirm both vertices exist and are distinct (vet1 != vet2).
- When loading edges, deduplicate/register all distinct vertices in a first pass, then add edges.
- After any removeVertex(), stop issuing edge calls that reference the removed vertex.
Example fix
// before: panics if a vertex is missing or vet1 == vet2
g.addEdge(a, b)
// after: ensure vertices exist and differ
if a != b {
g.addVertex(a)
g.addVertex(b)
g.addEdge(a, b)
} Defensive patterns
Strategy: validation
Validate before calling
// Membership helper (adjList is unexported; expose a method on the type).
func (g *graphAdjList) hasVertex(v Vertex) bool {
_, ok := g.adjList[v]
return ok
}
if vet1 != vet2 && g.hasVertex(vet1) && g.hasVertex(vet2) {
g.addEdge(vet1, vet2)
} Try / catch
defer func() {
if r := recover(); r != nil {
// edge rejected: missing vertex or self-loop
}
}()
g.addEdge(vet1, vet2) Prevention
- Register every distinct vertex with addVertex() before any addEdge().
- Reject or skip self-loop edges (vet1 == vet2) upstream.
- Build graphs in two passes: vertices first, then edges.
When it happens
Trigger: Calling addEdge() with a vertex that was never added via addVertex(); passing vet1 == vet2; adding an edge whose endpoint was previously removeVertex()'d.
Common situations: Building a graph from raw edge data and forgetting to register vertices first; reusing a graph instance after removing a vertex but still referencing it in edge calls; assuming the constructor registered a vertex that was actually skipped.
Related errors
- error
- Illegal Argument Exception
- Illegal Argument Exception
- Illegal Argument Exception
- Illegal Argument Exception
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/7c677ca86e7dc9c2.
Report an issue: GitHub.