mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 07:43:04 +08:00
Fix graph reverse method
This commit is contained in:
parent
f8a84c278b
commit
ede3b37825
@ -138,6 +138,7 @@ export default class Graph {
|
||||
*/
|
||||
reverse() {
|
||||
/** @param {GraphEdge} edge */
|
||||
const reversedEdges = [];
|
||||
this.getAllEdges().forEach((edge) => {
|
||||
// Delete straight edge from graph and from vertices.
|
||||
this.deleteEdge(edge);
|
||||
@ -145,7 +146,11 @@ export default class Graph {
|
||||
// Reverse the edge.
|
||||
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);
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user