当前位置:   article > 正文

【PTA】【数据结构与算法】并查集_the array representation of a disjoint set contain

the array representation of a disjoint set containing numbers 0 to 8 is give

选择题

1.The array representation of a disjoint set containing numbers 0 to 8 is given by { 1, -4, 1, 1, -3, 4, 4, 8, -2 }. Then to union the two sets which contain 6 and 8 (with union-by-size), the index of the resulting root and the value stored at the root are:
选项
A1 and -6
B4 and -5
C8 and -5
D8 and -6
2.The array representation of a disjoint set is given by { 4, 6, 5, 2, -3, -4, 3 }. If the elements are numbered from 1 to 7, the resulting array after invoking Union(Find(7),Find(1)) with union-by-size and path-compression is:
选项
A{ 4, 6, 5, 2, 6, -7, 3 }
B{ 4, 6, 5, 2, -7, 5, 3 }
C{ 6, 6, 5, 6, -7, 5, 5 }
D{ 6, 6, 5, 6, 6, -7, 5 }
3.The array representation of the disjoint sets is given by {2, –4, 2, 3, -3, 5, 6, 9, -2}. Keep in mind that the elements are numbered from 1 to 9. After invoking Union(Find(4), Find(6)) with union-by-size, which elements will be changed in the resulting array?
选项
Aa. elements of index 2 and 5
Ba. elements of index 2 and 6
Ca. elements of index 4 and 5
Da. elements of index 4 and 6
4.In a disjoint set problem, given a set of m elements S = { 1, 2, 3, …, m } and n ( 0<n<m ) distinct relations, the set S must have __ equivalence classes.
选项
Aat leatst m
Bexactly n
Cexactly m−n
Dat least m−n
5.已知不相交集合用数组表示为{ 4, 6, 5, 2, -3, -4, 3 }。若集合元素从1到7编号,则调用Union(Find(7),Find(1))(按规模求并,并且带路径压缩)后的结果数组为:
选项
A{ 4, 6, 5, 2, 6, -7, 3 }
B{ 4, 6, 5, 2, -7, 5, 3 }
C{ 6, 6, 5, 6, -7, 5, 5 }
D{ 6, 6, 5, 6, 6, -7, 5 }
6.若并查集用树表示,其中有n个结点,查找一个元素所属集合的算法的时间复杂度为____。
选项
AO(log2n)
BO(n)
CO(n2)
DO(nlog2n)
7.In a disjoint set problem, given a set of m elements S = { 1, 2, 3, …, m } and n ( 0<n<m ) distinct relations, the set S must have __ equivalence classes.
选项
Aat leatst m
Bexactly n
Cexactly m−n
Dat least m−n

填空题

1.本题要求给出下列并查集操作执行后,集合数组内存储的结果。
union( find(4), find(6) )
union( find(2), find(7) )
union( find(0), find(4) )
union( find(7), find(6) )
union( find(7), find(1) )
  • 1
  • 2
  • 3
  • 4
  • 5

注意:这里假设按规模求并(若两集合规模相等,则把第1个集合的根结点作为结果的根结点),并且用带路径压缩的查找。对所有的0≤i≤7,S[i]被初始化为−1。

i01234567
S[i]444-1-6-142

程序填空题

1.请填空完成下列代码,功能是实现并查集中的“查”,并且带路径压缩。
SetType Find ( ElementType X, DisjSet S )
{   
   ElementType root, trail, lead;

   for ( root = X; S[root] > 0; root = S[root]) ;  
   for ( trail = X; trail != root; trail = lead ) {
      lead = S[trail] ;   
      S[trail] = root;   
   } 
   return root;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/557628
推荐阅读
相关标签
  

闽ICP备14008679号