本文整理汇总了Python中UnionFind.UnionFind.items方法的典型用法代码示例。如果您正苦于以下问题:Python UnionFind.items方法的具体用法?Python UnionFind.items怎么用?Python UnionFind.items使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnionFind.UnionFind
的用法示例。
在下文中一共展示了UnionFind.items方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: UnionFindTestCase
# 需要导入模块: from UnionFind import UnionFind [as 别名]
# 或者: from UnionFind.UnionFind import items [as 别名]
class UnionFindTestCase(unittest.TestCase):
def setUp(self):
self.uf = UnionFind()
def test_insertion(self):
guys = ['nathan', 'mike', 'john', 'albert']
for guy in guys:
res = self.uf[guy]
assert res == guy
for guy in guys:
assert guy in self.uf.parents
def test_union(self):
self.test_insertion()
self.uf.union('nathan', 'mike')
self.uf.union('john', 'albert')
# parents ...
assert self.uf.parents['mike']['parent'] == self.uf.parents['nathan']['parent']
assert self.uf.parents['john']['parent'] == self.uf.parents['albert']['parent']
assert self.uf.parents['nathan']['parent'] != self.uf.parents['john']['parent']
assert self.uf.parents['nathan']['parent'] != self.uf.parents['albert']['parent']
assert self.uf.parents['john']['parent'] != self.uf.parents['nathan']['parent']
assert self.uf.parents['john']['parent'] != self.uf.parents['mike']['parent']
# shorter ;)
assert self.uf['mike'] == self.uf['nathan']
assert self.uf['john'] == self.uf['albert']
assert self.uf['nathan'] != self.uf['john']
assert self.uf['nathan'] != self.uf['albert']
assert self.uf['john'] != self.uf['nathan']
assert self.uf['john'] != self.uf['mike']
it_dict = {k: v for k, v in self.uf.items()}
assert 'john' in it_dict and it_dict['john'] == 'john'
assert 'albert' in it_dict and it_dict['albert'] == 'john'
assert 'nathan' in it_dict and it_dict['nathan'] == 'nathan'
assert 'mike' in it_dict and it_dict['mike'] == 'nathan'
it_dict.clear()
# weights
assert self.uf.parents['nathan']['weight'] == self.uf.parents['mike']['weight'] + 1 == 2
assert self.uf.parents['john']['weight'] == self.uf.parents['albert']['weight'] + 1 == 2
# one more union !
self.uf.union('mike', 'albert')
assert self.uf['mike'] == self.uf['albert']
self.uf.parents['albert']['weight'] == self.uf.parents['mike']['weight'] + 2 == 4
# now we shoud have only one set
it_dict = {k: v for k, v in self.uf.items()}
assert 'john' in it_dict and it_dict['john'] == 'nathan'
assert 'albert' in it_dict and it_dict['albert'] == 'nathan'
assert 'nathan' in it_dict and it_dict['nathan'] == 'nathan'
assert 'mike' in it_dict and it_dict['mike'] == 'nathan'
it_dict.clear()
def test_deunion(self):
self.test_union()
self.uf.deunion('nathan')
assert self.uf['nathan'] == self.uf['nathan'] # nathan was the parent of the set
assert self.uf['john'] != self.uf['nathan']
assert self.uf['mike'] != self.uf['nathan']
assert self.uf['albert'] != self.uf['nathan']
assert self.uf['john'] == self.uf['mike'] == self.uf['albert'] # those guys are still in the same set
# add nathan again
self.uf.union('nathan', 'mike')
# and try a deunion with multiple items
self.uf.deunion('albert', 'john')
assert self.uf['albert'] == self.uf['albert']
assert self.uf['john'] == self.uf['john']
assert self.uf['mike'] == self.uf['nathan'] != self.uf['albert'] != self.uf['john']
# de union everyone
self.uf.deunion('albert', 'john', 'mike', 'nathan')
assert self.uf['mike'] == self.uf['mike']
assert self.uf['john'] == self.uf['john']
assert self.uf['nathan'] == self.uf['nathan']
assert self.uf['albert'] == self.uf['albert']
def test_iter_sets(self):
self.test_deunion()
self.uf.deunion('albert', 'john', 'mike', 'nathan')
roots = ['albert', 'john', 'mike', 'nathan']
res = []
try:
self.uf.iter_sets()
except NotImplementedError: # implementation may not exists
return
for el in self.uf.iter_sets():
assert len(el) == 1
res.append(el[0])
assert(sorted(roots) == sorted(res))
self.uf.union('albert', 'john', 'mike')
for el in self.uf.iter_sets():
assert len(el) == 1 or len(el) == 3
if len(el) == 3:
assert set(['albert', 'john', 'mike']) == set(el)
else:
#.........这里部分代码省略.........