I'm working with an undirected graph where I need to frequently add and remove edges. I also need to efficiently determine the number of connected components after each operation.
For adding edges, I've found that using a Disjoint Set Union data structure provides a very efficient way to merge components and track the number of sets using the union operation.
However, removing an edge with a standard DSU seems to be less efficient. If an edge removal disconnects a component, the DSU doesn't inherently provide a way to easily split the sets. The typical approach seems to involve re-initializing the whole Disjoint Set Union, which is slow for a large number of edges.
Just to be clear I'll provide an example
Consider a graph with vertices V = {1, 2, 3, 4, 5} and initial edges E = {(1, 2), (2, 3), (4, 5)}. The connected components are {1, 2, 3} and {4, 5}.
Adding an edge (3, 4) results in new edges E' = {(1, 2), (2, 3), (4, 5), (3, 4)} and a single connected component {1, 2, 3, 4, 5}. Disjoint Set Union efficiently handles this.
Now, removing edge (2, 3) leaves edges E'' = {(1, 2), (4, 5), (3, 4)} and new connected components {1, 2} and {3, 4, 5}. The challenge is that using only a basic DSU I need to go though the whole graph to determine the connected components.
