algorithm template
c++
Union Find
Complexity
n = the number of vertices in the graph.
Time Complexity Space Complexity Constructor O(n) O(n) Find O(α(n)) O(1) Union O(α(n)) O(1) isConnected O(α(n)) O(1)
Note:
n
is the number of vertices in the graph. α
refers to the Inverse Ackermann function
. In practice, we assume it's a constant. In other words, O(α(n)) is regarded as O(1) as average.find
operation will take O(α(n)) time on average. Since union
and isConnected
both make calls to find
and all other operations require constant time, union
and isConnected
functions will also take O(α(n)) time on average.