本文整理汇总了Python中UnionFind.UnionFind.makeSet方法的典型用法代码示例。如果您正苦于以下问题:Python UnionFind.makeSet方法的具体用法?Python UnionFind.makeSet怎么用?Python UnionFind.makeSet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnionFind.UnionFind
的用法示例。
在下文中一共展示了UnionFind.makeSet方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: segmentIsland
# 需要导入模块: from UnionFind import UnionFind [as 别名]
# 或者: from UnionFind.UnionFind import makeSet [as 别名]
def segmentIsland(flatFaces,island):
sets = UnionFind(True)
if len(island)==0:
island = range(len(flatFaces))
for face in island:
if face not in sets.leader.keys():
sets.makeSet([face])
neighbor = flatFaces[face].fromFace
if neighbor != None:
if neighbor not in sets.leader.keys():
sets.makeSet([neighbor])
sets.union(face,neighbor)
return sets.group, sets.leader
示例2: range
# 需要导入模块: from UnionFind import UnionFind [as 别名]
# 或者: from UnionFind.UnionFind import makeSet [as 别名]
interval = range(A, B + 1)
#print A, B, P
print "sieve for ", B-A
primes = sieve(B - A)
print "trimming primes"
primes = [a for a in primes if a >= P]
print "calculating factors"
set_dict2 = {}
for prime in primes:
set_dict2[prime] = [a for a in interval if a % prime == 0]
#primes_dict = {item: calc_prime_factor(item, primes, P) for item in range(A, B+1)}
#print primes_dict
print "creating UnionFind"
set_list = UnionFind()
for item in range(A, B+1):
set_list.makeSet([item])
#set_dict = {prime: [] for prime in primes}
#print "creating sets"
#for item in range(A,B+1):
# for prime in primes_dict[item]:
# set_dict[prime].append(item)
#print set_dict
#print set_list.getNumGroups()
print "reducing sets"
for item in set_dict2:
temp_list = set_dict2[item]
if len(temp_list) > 1:
for new_item in temp_list:
set_list.union(temp_list[0],new_item)
results = set_list.getNumGroups()
#print results
示例3: open
# 需要导入模块: from UnionFind import UnionFind [as 别名]
# 或者: from UnionFind.UnionFind import makeSet [as 别名]
from UnionFind import UnionFind
import random
# build the graph
with open('data/clustering_big.txt') as f:
nodes = {}
clusters = UnionFind()
for i, line in enumerate(f.readlines()):
if i == 0:
N, numbits = line.split()
N, numbits = int(N), int(numbits)
else:
bits = line.strip().replace(" ", "")
nodes[bits] = True
clusters.makeSet([bits])
# run the algorithm
for k, node in enumerate(nodes.iterkeys()):
if k%1000 == 0:
print k
for i in xrange(numbits):
alt = node[:i] + node[i].replace(node[i], str(1-int(node[i]))) + node[i+1:]
if nodes.get(alt, False):
clusters.union(node, alt)
for j in xrange(i+1, numbits):
alt2 = alt[:j] + alt[j].replace(alt[j], str(1-int(alt[j]))) + alt[j+1:]
if nodes.get(alt2, False):
clusters.union(node, alt2)
print clusters.getNumGroups()