Union/find algorithm without union by rank for disjoint-set forests data structure

后端 未结 2 1492
轮回少年
轮回少年 2021-02-05 09:24

Here\'s a breakdown on the union/find algorithm for disjoint set forests on wikipedia:

  • Barebone disjoint-set forests... (O(n))
    • ... with unio
2条回答
  •  时光取名叫无心
    2021-02-05 10:11

    Path compression flattens the tree structure. Union by rank helps to merge. Assume that you skip the latter. So now, you have a forest with no rank information to choose how to merge. Potentially, you now run the risk of merging a tree with a larger depth to one with a smaller depth -- leading to an unbalanced tree structure. In the worst case, you may end up with a linked list. Your Union's amortized time complexity increases even if that for Find remains the same.

    IMO, It'd be better to skip path compression but not rank.

提交回复
热议问题