mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-12-19 08:59:05 +08:00
Make it possible to delete edge from graph.
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user