trekhleb/javascript-algorithms · error · Error
Edge not found in graph
Error message
Edge not found in graph
What it means
Graph.deleteEdge() removes the entry under edge.getKey() from this.edges and throws at Graph.js:103-107 when that key is absent - deleting an edge the graph does not hold is treated as a bug, not a no-op. It then also strips the edge from both endpoint vertices. Because keys are direction-sensitive ('start_end'), an edge can be missing even when the reverse pair exists in an undirected graph.
Source
Thrown at src/data-structures/graph/Graph.js:106
startVertex.addEdge(edge);
} else {
// If graph ISN'T directed then add the edge to both vertices.
startVertex.addEdge(edge);
endVertex.addEdge(edge);
}
return this;
}
/**
* @param {GraphEdge} edge
*/
deleteEdge(edge) {
// Delete edge from the list of edges.
if (this.edges[edge.getKey()]) {
delete this.edges[edge.getKey()];
} else {
throw new Error('Edge not found in graph');
}
// Try to find and end start vertices and delete edge from them.
const startVertex = this.getVertexByKey(edge.startVertex.getKey());
const endVertex = this.getVertexByKey(edge.endVertex.getKey());
startVertex.deleteEdge(edge);
endVertex.deleteEdge(edge);
}
/**
* @param {GraphVertex} startVertex
* @param {GraphVertex} endVertex
* @return {(GraphEdge|null)}
*/
findEdge(startVertex, endVertex) {
const vertex = this.getVertexByKey(startVertex.getKey());
View on GitHub (pinned to 85293e3e2b)
Solutions
- Fetch the stored edge and delete that instance: const stored = graph.findEdge(u, v); if (stored) graph.deleteEdge(stored); - findEdge() matches both orientations.
- Guard by key: if (graph.edges[edge.getKey()]) graph.deleteEdge(edge);
- Delete in a single pass over graph.getAllEdges() and do not re-enqueue deletions.
- Normalize endpoint order for undirected graphs before constructing the edge you delete.
Example fix
// before
graph.deleteEdge(edge);
graph.deleteEdge(edge); // Error: Edge not found in graph
// after
const stored = graph.findEdge(edge.startVertex, edge.endVertex);
if (stored) {
graph.deleteEdge(stored);
} Defensive patterns
Strategy: validation
Validate before calling
// findEdge() resolves either orientation and returns the stored instance
const stored = graph.findEdge(startVertex, endVertex);
if (stored) {
graph.deleteEdge(stored);
} Try / catch
try {
graph.deleteEdge(edge);
} catch (error) {
if (error.message === 'Edge not found in graph') {
// already deleted or never added - treat as an idempotent no-op
} else {
throw error;
}
} Prevention
- Delete only instances returned by getAllEdges()/findEdge(), never freshly constructed lookalikes.
- Make deletion single-pass; do not queue the same edge for removal twice.
- Remember keys are directional even in undirected graphs - normalize endpoint order or use findEdge().
- Do not reuse an edge object for deletion after calling reverse() on it.
When it happens
Trigger: Calling deleteEdge(edge) twice (the first call removes the key); deleting an edge that was never added; constructing new GraphEdge(b, a) to delete an undirected edge that was stored under the a_b key; deleting after edge.reverse() or after endpoint mutation changed getKey().
Common situations: Cleanup loops that visit the same edge twice (iterating a copied edge list while queueing deletions); mixed endpoint ordering between the add path and the delete path; tests that delete fixture edges repeatedly without rebuilding the graph.
Related errors
- One or two values are not in sets
- Vertex has already been added before
- Edge has already been added before
- Graph vertex must have a value
AI-assisted analysis of trekhleb/javascript-algorithms@85293e3e2b (2026-08-24).
Data as JSON: /api/errors/afaf6d7b30a8a9b7.
Report an issue: GitHub.