Make it possible to delete edge from graph.

This commit is contained in:
Oleksii Trekhleb
2018-05-11 16:58:39 +03:00
parent cb48efee79
commit 808a1e713f
2 changed files with 62 additions and 0 deletions

View File

@@ -88,6 +88,25 @@ export default class Graph {
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