Union Find
Data structure for undirected graphs that supports:
- edge addition: connect two vertices
- connectivity detection: determine whether two vertices are in the same connected component
__init__
Arguments
n: int: number of vertices
Complexities
- time:
- space:
root
Returns the representative of the connected component containing vertex k. Useful to assign connected components values such as potential.
Arguments
k: int: vertex
Returns
int: representative of the connected component containing vertexk
Complexities
unite
Add an edge spanning between vertices i and j.
Arguments
i: int: vertexj: int: vertex
Returns
bool:Trueif the edge was essentially added (iandjwere not connected),Falseotherwise
Complexities
is_connected
Returns whether vertices i and j are connected.
Arguments
i: int: vertexj: int: vertex
Returns
bool:Trueif verticesiandjare connected,Falseotherwise
Complexities
size
Returns the size of the connected component containing vertex k.
Arguments
k: int: vertex
Returns
int: size of the connected component containing vertexk
