ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2020-02-04 16:04:06  阅读:255  来源: 互联网

标签:node vacant perform 查集 相交 Find Remove nodes 节点


给并查集(不相交集)的添加一个\(Remove(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 \(Remove(X)\), first perform a \(Find(X)\) with path compression. Then mark the node containing \(X\) as vacant. Create a new one-node tree with \(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\) Removes are performed, perform a Find on every node, with path compression. If a \(Find(X)\) returns a vacant root, then place \(X\) in the root node, and make the old node containing \(X\) vacant. The results of guarantee that this will take linear time, which can be charged to the \(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 \(2N\) nodes in the forest and preserves the \(Mα(M, N)\) asymptotic time bound.

百度翻译

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

标签:node,vacant,perform,查集,相交,Find,Remove,nodes,节点
来源: https://www.cnblogs.com/geekfx/p/12259743.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有