并查集的按大小合并时间复杂度证明
可能比较粗糙。
我们要证明一个大小为
显然该命题对于
考虑归纳法,假设该命题对于
记不超过
-
若
n 为2 的幂:此时
2^{h(n)-1}\le |U|<n,0<|V|\le 2^{h(n)-1} ,有h(|U|)=h(n)-1,h(|V|)\le h(n)-1 ,所以\text{dep}_U\le h(n)-1,\text{dep}_V+1\le h(n) ,因此\text{dep}_W\le h(n)=\lfloor\log_2n\rfloor 。 -
若
n 不为2 的幂:-
若
2^{h(n)}\le |U|<n,0<|V|<2^{h(n)} ,则h(|U|)=h(n),h(|V|)\le h(n)-1 。所以\text{dep}_U\le h(n),\text{dep}_V+1\le h(n) ,因此\text{dep}_W\le h(n)=\lfloor \log_2n\rfloor 。 -
若
0<|V|\le |U|<2^{h(n)} ,则h(|V|)\le h(|U|)\le h(n)-1 ,所以\text{dep}_U\le h(n)-1,\text{dep}_V+1\le h(n) ,因此\text{dep}_W\le h(n)=\lfloor \log_2n\rfloor 。
-
证毕。