并查集(不相交集)的Remove操作

给并查集(不相交集)的添加一个 R e m o v e ( X ) Remove(X) Remove(X)操作,该操作把 X X X从当前的集合中除去并把它放到自己的集合中。

实现思想

英文原句

We assume that the tree is implemented with pointers instead of a simple array. Thus Find will return a pointer instead of an actual set name. We will keep an array to map set numbers to their tree nodes. Union and Find are implemented in the standard manner. To perform R e m o v e ( X ) Remove(X) Remove(X), first perform a F i n d ( X ) Find(X) Find(X) with path compression. Then mark the node containing X X X as vacant. Create a new one-node tree with X X X and have it pointed to by the appropriate array entry. The time to perform a Remove is the same as the time to perform a Find, except that there potentially could be a large number of vacant nodes. To take care of this, after N N N Removes are performed, perform a Find on every node, with path compression. If a F i n d ( X ) Find(X) Find(X) returns a vacant root, then place X X X in the root node, and make the old node containing X X X vacant. The results of guarantee that this will take linear time, which can be charged to the N N N Removes. At this point, all vacant nodes (indeed all nonroot nodes) are children of a root, and vacant nodes can be disposed (if an array of pointers to them has been kept). This also guarantees that there are never more than 2 N 2N 2N nodes in the forest and preserves the M α ( M , N ) Mα(M, N) Mα(M,N) asymptotic time bound.

百度翻译

我们假设树是用指针实现的,而不是简单的数组。因此 F i n d Find Find将返回一个指针,而不是实际的集合名。我们将保留一个数组来将集合编号映射到它们的树节点。 U n i o n Union Union F i n d Find Find以标准方式实现。要执行 R e m o v e ( X ) Remove(X) RemoveX,首先使用路径压缩执行 F i n d ( X ) Find(X) FindX。然后将包含 X X X的节点标记为空。用 X X X创建一个新的单节点树,并用适当的数组项指向它。执行移除的时间与执行查找的时间相同,只是可能存在大量空闲节点。为了解决这个问题,在执行 N N N个remove之后,在每个节点上执行一个 F i n d Find Find,并进行路径压缩。如果 F i n d ( X ) Find(X) FindX返回一个空根,那么将 X X X放在根节点中,并使包含 X X X的旧节点成为空节点。结果保证这将花费线性时间,这将被计入 N N N个移除。此时,所有空节点(实际上所有非根节点)都是根的子节点,可以释放空节点(如果保留了指向它们的指针数组)。这也保证了森林中不超过 2 N 2N 2N个节点,并保持了 M α ( M , N ) Mα(M,N) MαMN渐近时间界。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值