Skip to content

Commit c26a75e

Browse files
committed
Fix graph reverse method
1 parent f85fd64 commit c26a75e

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

src/data-structures/graph/Graph.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,19 @@ export default class Graph {
144144
*/
145145
reverse() {
146146
/** @param {GraphEdge} edge */
147+
const reversedEdges = [];
147148
this.getAllEdges().forEach((edge) => {
148149
// Delete straight edge from graph and from vertices.
149150
this.deleteEdge(edge);
150151

151152
// Reverse the edge.
152153
edge.reverse();
153154

154-
// Add reversed edge back to the graph and its vertices.
155+
// Add reversed edge to the list of reversed edges.
156+
reversedEdges.push(edge);
157+
});
158+
reversedEdges.forEach((edge) => {
159+
// Add reversed edge to the graph.
155160
this.addEdge(edge);
156161
});
157162

0 commit comments

Comments
 (0)