- Type Parameters:
T- element type
public class TreeDynamicConnectivity<T>
extends java.lang.Object
This data structure supports the following operations:
- Adding an element in $\mathcal{O}(\log 1)$
- Checking if an element in present in $\mathcal{O}(1)$
- Connecting two elements in $\mathcal{O}(\log n)$
- Checking if two elements are connected in $\mathcal{O}(\log n)$
- Removing connection between two nodes in $\mathcal{O}(\log n)$
- Removing an element in $\mathcal{O}(deg(element)\cdot\log n + 1)$
This data structure doesn't allow to store graphs with cycles. Also, the edges are considered to be undirected. The memory complexity is linear in the number of inserted elements. The implementation is based on the Euler tour technique.
For the description of the Euler tour data structure, we refer to the Monika Rauch Henzinger, Valerie King: Randomized dynamic graph algorithms with polylogarithmic time per operation. STOC 1995: 519-527
- Author:
- Timofey Chudakov
-
Constructor Summary
Constructors Constructor Description TreeDynamicConnectivity()Constructs a newTreeDynamicConnectivityinstance -
Method Summary
Modifier and Type Method Description booleanadd(T element)Adds anelementto this data structure.booleanconnected(T first, T second)Checks if thefirstandsecondbelong to the same tree.booleancontains(T element)Checks if this data structure contains theelement.booleancut(T first, T second)Removes an edge between thefirstandsecond.booleanlink(T first, T second)Adds an edge between thefirstandsecondelements.booleanremove(T element)Removes theelementfrom this data structure.
-
Constructor Details
-
TreeDynamicConnectivity
public TreeDynamicConnectivity()Constructs a newTreeDynamicConnectivityinstance
-
-
Method Details
-
add
Adds anelementto this data structure. If theelementhas been added before, this method returnsfalseand has no effect.This method has $\mathcal{O}(\log 1)$ running time complexity
- Parameters:
element- an element to add- Returns:
trueupon successful modification,falseotherwise
-
remove
Removes theelementfrom this data structure. This method has no effect if theelementhasn't been added to this data structureThis method has $\mathcal{O}(deg(element)\cdot\log n + 1)$ running time complexity
- Parameters:
element- an element to remove- Returns:
trueupon successful modification,falseotherwise
-
contains
Checks if this data structure contains theelement.This method has expected $\mathcal{O}(1)$ running time complexity
- Parameters:
element- an element to check presence of- Returns:
trueif theelementis stored in this data structure,falseotherwise
-
link
Adds an edge between thefirstandsecondelements. The method has no effect if the elements are already connected by some path, i.e. belong to the same tree. In the case some of the nodes haven't been added before, they're added to this data structure.This method has $\mathcal{O}(\log n)$ running time complexity
- Parameters:
first- an elementsecond- an element- Returns:
trueupon successful modification,falseotherwise
-
connected
Checks if thefirstandsecondbelong to the same tree. The method will returnfalseif either of the elements hasn't been added to this data structureThis method has $\mathcal{O}(\log n)$ running time complexity
- Parameters:
first- an elementsecond- an element- Returns:
trueif the elements belong to the same tree,falseotherwise
-
cut
Removes an edge between thefirstandsecond. This method doesn't have any effect if there's no edge between these elementsThis method has $\mathcal{O}(\log n)$ running time complexity
- Parameters:
first- an elementsecond- an element- Returns:
trueupon successful modification,falseotherwise
-