首页 > 技术文章 > 并查集

luojiahu 2014-08-08 14:48 原文

维基百科:

http://zh.wikipedia.org/zh-cn/并查集

假设初始化时用数组表示每个位置上的元素其father是自己,以对象是整数集为例

Init()

for i<- 1:n

  do father[i]<- i;

 

findFather(x)

if father[x] = x

  return father[x];

else

  return findFather(father[x]);

 

union(x,y)

fatherX <- findFather[x];

fatherY <- findFather[y];

if fatherX != fatherY

  father[x] = y;

return;

 

推荐阅读