Exemple #1
0
    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
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
Exemple #3
0
    def __init__(self, G, type):

        #build priority queue
        print len(G.E())
        pq = IndexMinPQ(len(G.E()), type)

        #sort edges
        for e in G.E():
            pq.insert(pq.N, e)

        uf = UnionFind(len(G.V()))

        while not pq.isEmpty() and len(self.mst) < len(G.V()) - 1:
            e = pq.min(
            )  #delete min operation. greedily add edge with smallest weight to MST
            print "minimum edge " + e.toString()
            pq.delMin()
            v = e.either()
            w = e.other(v)

            if not uf.connected(v, w):  #edge v-w does not create cycle
                uf.union(v, w)  #merge sets
                self.mst.append(e)  #add edge to MST
Exemple #4
0
from UnionFind import UnionFind

lines = """4 7
1 0 1
0 0 1
0 2 3
1 0 1
1 1 2
0 0 2
1 1 3"""
lines = lines.split("\n")
n, q = [int(i) for i in lines[0].split()]

disjoint = UnionFind(n)
for i in range(1, q + 1):
    typ, p, q = [int(j) for j in lines[i].split()]
    if typ == 0:
        disjoint.unify(p, q)
    else:
        print(1 if disjoint.connected(p, q) else 0)