本文整理汇总了Python中UnionFind.UnionFind.connected方法的典型用法代码示例。如果您正苦于以下问题:Python UnionFind.connected方法的具体用法?Python UnionFind.connected怎么用?Python UnionFind.connected使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnionFind.UnionFind
的用法示例。
在下文中一共展示了UnionFind.connected方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from UnionFind import UnionFind [as 别名]
# 或者: from UnionFind.UnionFind import connected [as 别名]
def run(self):
L = self.L
R = self.R
percolates = False
union = UF(L * L + 2)
# bottom
for i in range(1, L + 1):
union.union(i, 0)
# top
for i in range(L * L - L + 1, L * L + 1):
union.union(i, L * L + 1)
particles = set()
edges = set()
while len(particles) != self.N:
if percolates: break
p = random.randrange(L, L * L - L)
particles.add(p)
current_vertice = self.g.vertex(p)
self.vertices_color[current_vertice] = [0, 1, 0, 1]
x, y = self._get_pos(p)
for i in range(max(0, x - R), min(L, x + R + 1)):
if percolates: break
for j in range(max(0, y - R), min(L, y + R + 1)):
if (i, j) == (x, y): continue
if self._sqrdist((i, j), (x, y)) > R * R: continue
if i <= x:
from_, to_ = i * L + j, p
else:
from_, to_ = p, i * L + j
if i * L + j in particles or i == 0 or i == L - 1:
union.union(from_ + 1, to_ + 1)
edges.add((from_, to_))
if union.connected(L * L + 1, 0):
percolates = True
for edge in edges:
if union.connected(0, edge[0] + 1):
current_edge = self.g.add_edge(self.vertices[edge[0]], self.vertices[edge[1]])
self.pen[current_edge] = 3
if percolates:
self.edge_color[current_edge] = [0, 0, 1, 1]
else:
self.edge_color[current_edge] = [1, 0, 0, 1]
if percolates:
self.tests.append(len(particles))
return percolates
示例2: KruskalMST
# 需要导入模块: from UnionFind import UnionFind [as 别名]
# 或者: from UnionFind.UnionFind import connected [as 别名]
def KruskalMST(graph:EdgeWeightedGraph):
MST = []
EdgeSequence=[]
UF = UnionFind(graph.numVertices())
#Pre-processing the edges
PQ = []
for item in graph.adj:
PQ += graph.adj[item]
heapq.heapify(PQ)
while (len(PQ)>0) & (len(MST) < graph.numVertices()-1):
e = heapq.heappop(PQ)
v = e.either()
w = e.other(v)
if (UF.connected(v,w)):
continue
else:
UF.union(v,w)
heapq.heappush(MST, e)
EdgeSequence.append(e)
cluster = set()
for item in UF.id:
cluster.add(item)
print ("cluster:", cluster, len(cluster))
print (e.weight)
return MST