这个星期天我要考试,我只是想确认我所做的是否正确(你知道考试让我怀疑)
这是算法的工作原理:
int Find(int x)
{ // Return the set containing x and compress the path from x to root
// First, find the root
int z = x; while (P[z] > 0) { z = P[z]; } int root = z;
// Start at x again and reset the parent // of every node on the path from x to root z = x;
while (P[z] > 0)
{ int t = P[z];
P[z] = root; // Reset Parent of z to root
z = t; }
return root; }
这是问题:
回想一下为一组 n 个元素的不相交集上的 Union-Find 问题开发的算法。Find 使用路径压缩,Union 使用排名。此外,相同等级的两棵树的联合选择与第二个参数关联的根作为新根。从集合 S = {1, 2, ..., 10} 和 10 个不相交的子集开始,每个子集包含一个元素。一个。执行后绘制最后一组树:
Union(1,2)、Union(3,4)、Union(5,6)、Union(1,5)、Union(1,3)。
这是我对这个问题的解决方案:
我很想有关于这个算法的任何提示或建议。
谢谢!