https://github.com/strilanc/unionfind-.net
A simple implementation of a disjoint set data structure in C#.
https://github.com/strilanc/unionfind-.net
Last synced: about 1 year ago
JSON representation
A simple implementation of a disjoint set data structure in C#.
- Host: GitHub
- URL: https://github.com/strilanc/unionfind-.net
- Owner: Strilanc
- License: unlicense
- Created: 2014-02-26T00:07:48.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2014-02-26T06:45:39.000Z (over 12 years ago)
- Last Synced: 2024-04-14T23:21:38.731Z (about 2 years ago)
- Language: C#
- Size: 133 KB
- Stars: 6
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Installation
============
1. Copy the source file `UnionFindNode.cs` into your project.
Algorithm
=========
The algorithm is sourced from [wikipedia's disjoint set data structure article](http://en.wikipedia.org/wiki/Union_find). Operations take amortized nearly constant time.
Usage
=====
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:
1. Add the field `private readonly UnionFindNode _connectedComponentNode = new UnionFindNode()` to `FancyGraphNode`.
2. When adding an edge, call `edge.Node1._connectedComponentNode.Union(edge.Node2._connectedComponentNode)`.
3. To determine if two nodes are in the same component, evaluate `edge.Node1._connectedComponentNode.IsUnionedWith(edge.Node2._connectedComponentNode)`.