Fix graph reverse method

This commit is contained in:
AmirMohammad Hosseini Nasab 2022-07-30 15:10:21 +04:30 committed by Amir Hosseini
parent f8a84c278b
commit ede3b37825

View File

@ -138,6 +138,7 @@ export default class Graph {
*/ */
reverse() { reverse() {
/** @param {GraphEdge} edge */ /** @param {GraphEdge} edge */
const reversedEdges = [];
this.getAllEdges().forEach((edge) => { this.getAllEdges().forEach((edge) => {
// Delete straight edge from graph and from vertices. // Delete straight edge from graph and from vertices.
this.deleteEdge(edge); this.deleteEdge(edge);
@ -145,7 +146,11 @@ export default class Graph {
// Reverse the edge. // Reverse the edge.
edge.reverse(); edge.reverse();
// Add reversed edge back to the graph and its vertices. // Add reversed edge to the list of reversed edges.
reversedEdges.push(edge);
});
reversedEdges.forEach((edge) => {
// Add reversed edge to the graph.
this.addEdge(edge); this.addEdge(edge);
}); });