日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

数据结构之并查集:并查集的介绍与Python代码实现——18

發(fā)布時間:2024/7/5 python 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构之并查集:并查集的介绍与Python代码实现——18 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

并查集的介紹

  • 并查集(Union-find)數(shù)據(jù)結(jié)構(gòu)也稱作合并查找集(Merge-find set)或者不相交集數(shù)據(jù)結(jié)構(gòu)(disjoint-set data structure),它是一種記錄了由一個或多個元素組成的不連續(xù)的分組的集合。并查集提供常數(shù)量的復(fù)雜度來添加、合并以及確定兩個元素是否屬于同一個集合。
  • 并查集除了能夠?qū)崿F(xiàn)這些快速便利的操作,它在Krukal算法中尋找最小生成樹的圖也起著關(guān)鍵的作用

    并查集結(jié)構(gòu)
    并查集是一種樹形數(shù)據(jù)結(jié)構(gòu),但是它和二叉樹、紅黑樹、B樹不同,它對樹形結(jié)構(gòu)的要求十分簡單:
  • 同一個數(shù)據(jù)組中的元素對應(yīng)在同一顆樹(判斷兩個元素是否在同一顆樹時,可以判斷根結(jié)點(diǎn)是否相同)
  • 同一數(shù)據(jù)組中的每一個元素對應(yīng)樹中的一個結(jié)點(diǎn)
  • 不同數(shù)據(jù)組之間不存在任何相關(guān)的聯(lián)系,可以看做多個數(shù)據(jù)組成一個森林(合并數(shù)據(jù)組時,只需要將一棵樹的根結(jié)點(diǎn)指向另一顆樹的根結(jié)點(diǎn)即可)
  • 樹中的結(jié)點(diǎn)不存在硬性的順序/父子關(guān)系

并查集的功能實(shí)現(xiàn)

方法設(shè)計

  • self.groups 是一個列表,其索引代表傳入的元素的值,其元素代表傳入元素所在分組
  • count_groups() 返回并查集中組的數(shù)量
  • which_group(item) 判斷傳入的元素item屬于哪一個群組
  • in_the_same_group(item1, item2) 判斷傳入的兩個元素是否在同一個分組
  • merge(item1, item2) 將兩個元素各自所在的分組合并到一個組,合并后的分組為后者所在的組
  • Python代碼實(shí)現(xiàn)

    class UnionFind:def __init__(self, n):self.num_groups = nself.groups = [i for i in range(n)] # Let the indices be the elements, and the elements be the group numbersdef count_groups(self):return self.num_groupsdef which_group(self, item):"""The indices are items, the elements are group numbers"""return self.groups[item]def in_the_same_group(self, item1, item2):return self.which_group(item1) == self.which_group(item2)def merge(self, item1, item2):group1 = self.which_group(item1)group2 = self.which_group(item2)if group1 == group2:returnfor i in range(len(self.groups)):if self.groups[i] == group1:self.groups[i] = group2self.num_groups -= 1

    代碼測試

    if __name__ == '__main__':UF = UnionFind(5)print(f"The initial number of groups is {UF.num_groups}")print(f"The initial groups is {UF.groups}")while True:p = int(input(f'Input the to-be-merge element: '))q = int(input(f"Merge to the target element's group: "))if UF.in_the_same_group(p, q):print(f"They are already in the same group")continueUF.merge(p, q)print(f"The number of groups now is {UF.count_groups()}")print(UF.groups)

    測試結(jié)果

    The initial number of groups is 5 The initial groups is [0, 1, 2, 3, 4] Input the to-be-merge element: 0 Merge to the target element's 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 element's group: 2 The number of groups now is 3 [2, 2, 2, 3, 4] Input the to-be-merge element: 2 Merge to the target element's group: 3 The number of groups now is 2 [3, 3, 3, 3, 4] Input the to-be-merge element: 3 Merge to the target element's group: 4 The number of groups now is 1 [4, 4, 4, 4, 4] Input the to-be-merge element:

    最少需要n-1 = 4 次合并,就可以將所有元素都合并到一個分組到一個組。

    創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

    總結(jié)

    以上是生活随笔為你收集整理的数据结构之并查集:并查集的介绍与Python代码实现——18的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。