Algs4-1.5.8一个反例证明

1.5.8用一个反例证明quick-find算法中的union()方法的以下直观实现是错误的:

public void union(int p,int q)

{

   if (connected(p,q) return;

  //将p的分量重命名为q的分量

  for (int i=0;i<id.length;i++)

      if (id[i]==id[p]) id[i]=id[q];

  count--;

}

答:反例:触点0,1,2,分量名0,0,2,p=0,q=2时,for循环结束后分量名为2,0,2,没有将分量名0全部更名成2。只会将触点p相同分量名中触点小于等于p的分量更名,相同分量中大于p的触点的分量不会更名。

猜你喜欢

转载自www.cnblogs.com/longjin2018/p/9854709.html
今日推荐