TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | Custom |
ReleasedLast Release | Dec 2014 |
Maintained by Unclaimed.
This is a tiny library implementing a union find / disjoint set data structure, featuring:
UFDisjointSetNode
. A UFDisjointSetNode
is a member of some implicit set of nodes. At any given time, the set is represented by some single specific node among its members. Sets never partially overlap; they are either the same set or have no nodes in common.unionWith:
to merge the sets two UFDisjointSetNode
s are members of into a single set.isInSameSetAs:
to determine if two UFDisjointSetNode
s are in the same set. Use currentRepresentative
to get the current node representing the set the receiving node is in. Nodes are in the same set when they have the same representative.Method #1: CocoaPods
pod 'UnionFind'
pod 'UnionFind', '~> 1.0'
pod install
from a terminal in your project directory#import "UnionFind.h"
wherever you want to access the library's types or methodsMethod #2: Manual
#import "UnionFind.h"
wherever you want to access the library's types or methodsThe 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 UFDisjointSetNode
. 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:
@private UFDisjointSetNode* _ccNode
to FancyGraphNode
init
function: _ccNode = [UFDisjointSetNode new]
.[edge.Node1._ccNode unionWith:edge.Node2._ccNode]
.[node1._ccNode isInSameSetAs:node2._ccNode]
.An example application is discussed in this blog post about incremental cycle detection.