/* ==========================================
 * JGraphT : a free Java graph-theory library
 * ==========================================
 *
 * Project Info:  http://jgrapht.sourceforge.net/
 * Project Creator:  Barak Naveh (http://sourceforge.net/users/barak_naveh)
 *
 * (C) Copyright 2003-2010, by Barak Naveh and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */
/* -------------------------
 * UnionFind.java
 * -------------------------
 * (C) Copyright 2010-2010, by Tom Conerly and Contributors.
 *
 * Original Author:  Tom Conerly
 * Contributor(s):
 *
 * Changes
 * -------
 * 02-Feb-2010 : Initial revision (TC);
 *
 */
package org.jgrapht.alg.util;

import java.util.*;


/**
 * An implementation of <a
 * href="http://en.wikipedia.org/wiki/Disjoint-set_data_structure">Union
 * Find</a> data structure. Union Find is a disjoint-set data structure. It
 * supports two operations: finding the set a specific element is in, and
 * merging two sets. The implementation uses union by rank and path compression
 * to achieve an amortized cost of O(a(n)) per operation where a is the inverse
 * Ackermann function. UnionFind uses the hashCode and equals method of the
 * elements it operates on.
 *
 * @author Tom Conerly
 * @since Feb 10, 2010
 */
public class UnionFind<V>
{
    //~ Instance fields --------------------------------------------------------

    private Map<V, V> parentMap;
    private Map<V, Integer> rankMap;

    //~ Constructors -----------------------------------------------------------

    /**
     * Creates a UnionFind instance with all of the elements of elements in
     * seperate sets.
     */
    public UnionFind(Set<V> elements)
    {
        parentMap = new HashMap<V, V>();
        rankMap = new HashMap<V, Integer>();
        for (V element : elements) {
            parentMap.put(element, element);
            rankMap.put(element, 0);
        }
    }

    //~ Methods ----------------------------------------------------------------

    /**
     * Adds a new element to the data structure in its own set.
     *
     * @param element The element to add.
     */
    public void addElement(V element)
    {
        parentMap.put(element, element);
        rankMap.put(element, 0);
    }

    /**
     * Returns the representative element of the set that element is in.
     *
     * @param element The element to find.
     *
     * @return The element representing the set the element is in.
     */
    public V find(V element)
    {
        if (!parentMap.containsKey(element)) {
            throw new IllegalArgumentException(
                "elements must be contained in given set");
        }

        V parent = parentMap.get(element);
        if (parent.equals(element)) {
            return element;
        }

        V newParent = find(parent);
        parentMap.put(element, newParent);
        return newParent;
    }

    /**
     * Merges the sets which contain element1 and element2.
     *
     * @param element1 The first element to union.
     * @param element2 The second element to union.
     */
    public void union(V element1, V element2)
    {
        if (!parentMap.containsKey(element1)
            || !parentMap.containsKey(element2))
        {
            throw new IllegalArgumentException(
                "elements must be contained in given set");
        }

        V parent1 = find(element1);
        V parent2 = find(element2);

        //check if the elements are already in the same set
        if (parent1.equals(parent2)) {
            return;
        }

        int rank1 = rankMap.get(parent1);
        int rank2 = rankMap.get(parent2);
        if (rank1 > rank2) {
            parentMap.put(parent2, parent1);
        } else if (rank1 < rank2) {
            parentMap.put(parent1, parent2);
        } else {
            parentMap.put(parent2, parent1);
            rankMap.put(parent1, rank1 + 1);
        }
    }
}

// End UnionFind.java

@@ -1980,17 +1980,17 @@
 ionFind%3C
-V
+T
 %3E%0A%7B%0A    
@@ -2083,20 +2083,20 @@
 ate Map%3C
-V, V
+T, T
 %3E parent
@@ -2112,25 +2112,25 @@
 private Map%3C
-V
+T
 , Integer%3E r
@@ -2359,17 +2359,17 @@
 ind(Set%3C
-V
+T
 %3E elemen
@@ -2414,12 +2414,12 @@
 Map%3C
-V, V
+T, T
 %3E();
@@ -2449,17 +2449,17 @@
 HashMap%3C
-V
+T
 , Intege
@@ -2477,17 +2477,17 @@
    for (
-V
+T
  element
@@ -2838,17 +2838,17 @@
 Element(
-V
+T
  element
@@ -2928,32 +2928,312 @@
 ent, 0);%0A    %7D%0A%0A
+    /**%0A     * @return map from element to parent element%0A     */%0A    protected Map%3CT, T%3E getParentMap()%0A    %7B%0A        return parentMap;%0A    %7D%0A%0A    /**%0A     * @return map from element to rank%0A     */%0A    protected Map%3CT, Integer%3E getRankMap()%0A    %7B%0A        return rankMap;%0A    %7D%0A%0A
     /**%0A     * R
@@ -3444,16 +3444,16 @@
 lic 
-V
+T
  find(
-V
+T
  ele
@@ -3634,25 +3634,25 @@
  %7D%0A%0A        
-V
+T
  parent = pa
@@ -3758,17 +3758,17 @@
         
-V
+T
  newPare
@@ -4070,17 +4070,17 @@
 d union(
-V
+T
  element
@@ -4082,17 +4082,17 @@
 ement1, 
-V
+T
  element
@@ -4327,25 +4327,25 @@
  %7D%0A%0A        
-V
+T
  parent1 = f
@@ -4371,9 +4371,9 @@
     
-V
+T
  par

Add protected accessors to UnionFind