当前位置: 首页 > news >正文

驾校视频网站模板大同建设局网站

驾校视频网站模板,大同建设局网站,网址地址查询,技术支持 网站建设并查集的优化 在上一节了解到并查集的快速查询#xff0c;合并#xff0c;判断归属组等操作#xff0c;虽然这些操作都非常方便#xff0c;但是在数据量较大的情况下#xff0c;并查集的效率并不算高#xff1a; 上一节中实现代码中使用的合并方法(merge#xff0c;AP…并查集的优化 在上一节了解到并查集的快速查询合并判断归属组等操作虽然这些操作都非常方便但是在数据量较大的情况下并查集的效率并不算高 上一节中实现代码中使用的合并方法(mergeAPI设计中为union)每次合并都需要遍历全部元素的次数而最少要合并N-1次才能将所有元素合并到同一组因此我们要对其合并进行优化 为了提升union算法的性能,我们需要重新设计find方法和merge方法的实现,此时我们先需要对我们的之前数据结构中的groups数组的含义进行重新设定。 使用 优化查询分组which_group方法 我们仍然让groups数组的索引作为结点的元素groups[i]的值不再是当前结点所在的分组标识而是当前结点的父结点根据当前结点的父结点可以找到祖父结点一直往上找直到找到它的根结点根结点则是其真正的分组 Python代码实现及测试 class UF_Tree:def __init__(self, n):self.num_groups nself.groups [i for i in range(n)]def count_groups(self):return self.num_groupsdef in_the_same_group(self, item1, item2):return self.which_group(item1) self.which_group(item2)def which_group(self, item):Find items root------groups[groups[groups[...groups[item]...]]]while self.groups[item] ! item:item self.groups[item]return itemdef merge(self, item1, item2):if self.in_the_same_group(item1, item2):returnself.groups[self.which_group(item1)] self.groups[self.which_group(item2)]self.num_groups - 1if __name__ __main__:UF UF_Tree(5)print(fThe initial number of groups is {UF.num_groups})print(fThe initial number of groups is {UF.groups})while True:p int(input(fInput the to-be-merge element: ))q int(input(fMerge to the target elements group: ))if UF.in_the_same_group(p, q):print(fThey are already in the same group)continueUF.merge(p, q)print(fThe number of groups now is {UF.count_groups()})print(UF.groups)运行结果 The initial number of groups is 5 The initial number of groups is [0, 1, 2, 3, 4] Input the to-be-merge element: 0 Merge to the target elements group: 1 The number of groups now is 4 [1, 1, 2, 3, 4] Input the to-be-merge element: 1 Merge to the target elements group: 2 The number of groups now is 3 [1, 2, 2, 3, 4] Input the to-be-merge element: 2 Merge to the target elements group: 3 The number of groups now is 2 [1, 2, 3, 3, 4] Input the to-be-merge element: 3 Merge to the target elements group: 4 The number of groups now is 1 [1, 2, 3, 4, 4] Input the to-be-merge element: 可以寻找到item 0的源分组为4
http://www.pierceye.com/news/976334/

相关文章:

  • 泰安网站推广优化网站百度收录是什么意思
  • 免费虚拟主机控制面板长沙推广优化公司
  • 重庆企业网站建站公司建站系统
  • 规划设计网站推荐自己做采集电影网站
  • 免费学编程的网站有哪些做网站设计用什么软件
  • 网站成品超市学室内设计去哪好
  • 网站备案一个主体无锡建设银行网站
  • delphi xe10网站开发台州做网站多少钱
  • 怎样设计卖奖的网站做电影网站哪个服务器好
  • 找外包公司做网站的好处和坏处wordpress 访问量大
  • 淄博 网站设计越秀公司网站建设
  • 网站该如何做本地网站搭建软件
  • 如何做汽车团购网站学做馒头面包哪个网站好
  • 中国科技成就总结莱芜网站优化排名公司
  • 中国建设银行网站企业网银收费北京网站建设小程序开发
  • 成交型网站倡导公司网络营销课程总结1000字
  • 网站建设注册前端开发培训机构推荐
  • 遵义网站推广中国房地产app下载安装最新版
  • c语言网站建设禅城网站开发
  • 宁波品牌网站制作哪家好太平阳电脑网网站模板
  • seo网站外链工具看设计比较好的网站
  • 济南道驰网站建设有限公司怎么样某网站网站的设计与实现
  • 服装印花图案设计网站设计一个网站要多久
  • ai怎么做自己的网站getpage wordpress使用详解
  • 龙岩做网站推广龙岗网站 建设深圳信科
  • 沈阳网站建设 网络服务广告公司首页
  • 旅游网站建设导航栏中国哪里正在大开发大建设
  • 哪能建设网站建设网站需要哪些流程图
  • 网站YYQQ建设o2o型网站
  • 给客户做网站 赚钱吗赣州省住房和城乡建设厅网站