- Copy the source file
UnionFindNode.cs
into your project.
The algorithm is sourced from wikipedia's disjoint set data structure article. Operations take amortized nearly constant time.
In the class that you want to union together, add a field of type UnionFindNode
. Initialize the node, either eagerly when the class is constructed or lazily just before it is needed, then perform operations on it.
For example, suppose we have a FancyGraphNode
to which edges can be added but not removed. We want to track if nodes are in the same connected component. We can:
- Add the field
private readonly UnionFindNode _connectedComponentNode = new UnionFindNode()
toFancyGraphNode
. - When adding an edge, call
edge.Node1._connectedComponentNode.Union(edge.Node2._connectedComponentNode)
. - To determine if two nodes are in the same component, evaluate
edge.Node1._connectedComponentNode.IsUnionedWith(edge.Node2._connectedComponentNode)
.