Package org.graphstream.algorithm.util
Class DisjointSets<E>
java.lang.Object
org.graphstream.algorithm.util.DisjointSets<E>
- Type Parameters:
E
- the type of the elements
public class DisjointSets<E> extends Object
This data structure is used to maintain disjoint sets. It supports limited number of operations, but they are all executed in constant amortized time. The supported operations are:
- Adding a new set containing a single element
- Determining if two elements belong to the same set
- Union of the sets containing two elements
The space taken by this structure is O(n), where n is the number of elements.
-
Constructor Summary
Constructors Constructor Description DisjointSets()
Creates a new instance containing no sets and no elementsDisjointSets(int initialCapacity)
Creates a new instance containing no sets and no elements. -
Method Summary
Modifier and Type Method Description boolean
add(E e)
Adds a new set containing onlye
to the structure.void
clear()
Reinitializes the structure.boolean
contains(Object e)
Checks if an element belongs to some of the disjoint sets.boolean
inSameSet(Object e1, Object e2)
Checks if two elements belong to the same set.boolean
union(Object e1, Object e2)
Union of the set containinge1
and the set containinge2
.
-
Constructor Details
-
DisjointSets
public DisjointSets()Creates a new instance containing no sets and no elements -
DisjointSets
public DisjointSets(int initialCapacity)Creates a new instance containing no sets and no elements. If the total number of elements to add is known in advance, this constructor is more efficient than the default.- Parameters:
initialCapacity
- Initial capacity (in number of elements) of the structure. The structure grows dynamically and new elements can be added even if this capacity is exceeded.
-
-
Method Details
-
add
Adds a new set containing onlye
to the structure. Ife
already belongs to some of the disjoint sets, nothing happens.- Parameters:
e
- The element to add as a singleton- Returns:
- True if the new set is added and false if
e
already belongs to some set.
-
inSameSet
Checks if two elements belong to the same set.- Parameters:
e1
- An elemente2
- An element- Returns:
- True if and only if belong to the same set. Note that if
e1
ore2
do not belong to any set, false is returned.
-
union
Union of the set containinge1
and the set containinge2
. After this operationinSameSet(e1, e2)
will return true. Ife1
ore2
do not belong to any set or if they already belong to the same set, nothing happens.- Parameters:
e1
- An elemente2
- An element- Returns:
true
if and only ife1
ande2
belong to different sets at the beginning
-
contains
Checks if an element belongs to some of the disjoint sets.- Parameters:
e
- An element- Returns:
- True if
e
belongs to some set.
-
clear
public void clear()Reinitializes the structure. After this operation the structure contains no sets.
-