Пример #1
0
 public void Unite(UnionFindNode<T> other) {
     UnionFindNode<T> a = this.Find();
     UnionFindNode<T> b = other.Find();
     if (a != b) {
         // need to unite a and b - a will be parent of b
         int aSize = a.subNodes == null ? 1 : a.subNodes.Count;
         int bSize = b.subNodes == null ? 1 : b.subNodes.Count;
         if (aSize < bSize) { // a is smaller, so it should be child
             UnionFindNode<T> t = a;
             a = b;
             b = t;
         }
         b.parent = a;
         if (a.subNodes == null) {
             a.subNodes = new List<UnionFindNode<T>>();
             a.subNodes.Add(a);
         }
         if (b.subNodes == null) {
             a.subNodes.Add(b);
         } else {
             foreach (UnionFindNode<T> bn in b.subNodes) {
                 a.subNodes.Add(bn);
             }
             b.subNodes = null;
         }
         allRoots.Remove(b);
     }
 }
Пример #2
0
 public UnionFind(IEnumerable <T> values)
 {
     foreach (T value in values)
     {
         UnionFindNode <T> n = new UnionFindNode <T>(roots, value);
         nodes.Add(n);
         nodeMap[value] = n;
         roots.Add(n);
     }
 }
Пример #3
0
 public IEnumerable<UnionFindNode<T>> GetSetMembers() {
     UnionFindNode<T> a = this.Find();
     if (a.subNodes == null) {
         List<UnionFindNode<T>> result = new List<UnionFindNode<T>>();
         result.Add(a);
         a.subNodes = result;
         return result;
     } else {
         return a.subNodes;
     }
 }
Пример #4
0
 private UnionFindNode<T> Find() {
     UnionFindNode<T> ret = this.parent;
     if (ret == null) {
         return this;
     } else {
         UnionFindNode<T> next = ret.parent;
         while (next != null) {
             ret = next;
             next = ret.parent;
         }
         this.parent = ret;
         return ret;
     }
 }
Пример #5
0
 public bool IsInSameSet(UnionFindNode<T> other) {
     return this.Find() == other.Find();
 }