Skip to content

Commit ede3b37

Browse files
committed
Fix graph reverse method
1 parent f8a84c2 commit ede3b37

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/data-structures/graph/Graph.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,19 @@ export default class Graph {
138138
*/
139139
reverse() {
140140
/** @param {GraphEdge} edge */
141+
const reversedEdges = [];
141142
this.getAllEdges().forEach((edge) => {
142143
// Delete straight edge from graph and from vertices.
143144
this.deleteEdge(edge);
144145

145146
// Reverse the edge.
146147
edge.reverse();
147148

148-
// Add reversed edge back to the graph and its vertices.
149+
// Add reversed edge to the list of reversed edges.
150+
reversedEdges.push(edge);
151+
});
152+
reversedEdges.forEach((edge) => {
153+
// Add reversed edge to the graph.
149154
this.addEdge(edge);
150155
});
151156

0 commit comments

Comments
 (0)