コード例 #1
0
ファイル: mst.py プロジェクト: weed478/asd1
def kruskal(G, n):
    G.sort(key=weight)
    vert = [Node(i) for i in range(n)]
    S = []

    for e in G:
        if find(vert[e[0]]) != find(vert[e[1]]):
            union(vert[e[0]], vert[e[1]])
            S.append((e[0], e[1]))

    return S
コード例 #2
0
def kruskal(G, id):
    aristas = []
    resultado = []
    for arista in G:
        costo, nodo, vecino = arista
        hq.heappush(aristas, (costo, nodo, vecino))

    while len(aristas):
        costo, u, v = hq.heappop(aristas)
        pu = find(id, u)
        pv = find(id, v)
        if pu != pv:
            resultado.append((costo, u, v))
            union(id, pu, pv)
    return resultado
コード例 #3
0
ファイル: kruskal.py プロジェクト: bradyz/sandbox
def kruskal(g):
    # number of vertices
    v = len(set([x.a for x in g]) | set([x.b for x in g]))

    # disjoin union data structure for subsets
    p = [-1 for _ in range(v)]

    # result set of edges
    res = []

    # add disjoint edges by weight
    for e in sorted(g, key=lambda x: x.w):
        if find(p, e.a.v) != find(p, e.b.v):
            res.append(e)
            union(p, e.a.v, e.b.v)

    return res
コード例 #4
0
ファイル: interpreter.py プロジェクト: tepru/piedit
    def find_color_blocks(self):
        """Uses the connected component algorithm to build the program color blocks."""
        next_label = 0
        #Pass 1
        for y in xrange(self.height):
            for x in xrange(self.width):
                pixel = self.pixels[x][y]
                if not self.is_background(pixel.color):
                    neighbours = self.neighbours(pixel)

                    if neighbours == []:
                        pixel.parent = self.pixels[x][y]
                        pixel.set_label = next_label
                        next_label = next_label + 1
                    else:
                        for n in neighbours:
                            unionfind.union(n, pixel)

        #Pass 2
        for y in xrange(self.height):
            for x in xrange(self.width):
                pixel = self.pixels[x][y]
                if not self.is_background(pixel.color):
                    root = unionfind.find(pixel)
                    pixel.set_size = root.set_size
                    pixel.set_label = root.set_label
                    #Build color block object
                    if not self.color_blocks.has_key(pixel.set_label):
                        self.color_blocks[pixel.set_label] = ColorBlock(
                            pixel.set_size)
                    self.color_blocks[pixel.set_label].update_boundaries(pixel)

        #Debug
        for i, color_block in self.color_blocks.items():
            bounds = color_block.boundary_pixels
            self.debug.writeln("Color Block %s: Size=%s, \n\tmaxRL=(%s,%s), maxRR=(%s,%s), \n\tmaxDL=(%s,%s), maxDR=(%s,%s), \n\tmaxLL=(%s,%s), maxLR=(%s,%s), \n\tmaxUL=(%s,%s), maxUR=(%s,%s)" \
                % (i, color_block.size,
                   bounds[0][0].x,bounds[0][0].y, bounds[0][1].x, bounds[0][1].y,
                   bounds[1][0].x,bounds[1][0].y, bounds[1][1].x, bounds[1][1].y,
                   bounds[2][0].x,bounds[2][0].y, bounds[2][1].x, bounds[2][1].y,
                   bounds[3][0].x,bounds[3][0].y, bounds[3][1].x, bounds[3][1].y))
コード例 #5
0
ファイル: interpreter.py プロジェクト: whilefalse/Piedit
 def find_color_blocks(self):
     """Uses the connected component algorithm to build the program color blocks."""
     next_label = 0
     #Pass 1
     for y in xrange(self.height):
         for x in xrange(self.width):
             pixel = self.pixels[x][y]
             if not self.is_background(pixel.color):
                 neighbours = self.neighbours(pixel)
                 
                 if neighbours == []:
                     pixel.parent = self.pixels[x][y]
                     pixel.set_label = next_label
                     next_label = next_label + 1
                 else:
                     for n in neighbours:
                         unionfind.union(n,pixel)
     
     #Pass 2
     for y in xrange(self.height):
         for x in xrange(self.width):
             pixel = self.pixels[x][y]
             if not self.is_background(pixel.color):
                 root = unionfind.find(pixel)
                 pixel.set_size = root.set_size
                 pixel.set_label = root.set_label
                 #Build color block object
                 if not self.color_blocks.has_key(pixel.set_label):
                     self.color_blocks[pixel.set_label] = ColorBlock(pixel.set_size)
                 self.color_blocks[pixel.set_label].update_boundaries(pixel)
 
     #Debug
     for i,color_block in self.color_blocks.items():
         bounds = color_block.boundary_pixels
         self.debug.writeln("Color Block %s: Size=%s, \n\tmaxRL=(%s,%s), maxRR=(%s,%s), \n\tmaxDL=(%s,%s), maxDR=(%s,%s), \n\tmaxLL=(%s,%s), maxLR=(%s,%s), \n\tmaxUL=(%s,%s), maxUR=(%s,%s)" \
             % (i, color_block.size, 
                bounds[0][0].x,bounds[0][0].y, bounds[0][1].x, bounds[0][1].y,
                bounds[1][0].x,bounds[1][0].y, bounds[1][1].x, bounds[1][1].y,
                bounds[2][0].x,bounds[2][0].y, bounds[2][1].x, bounds[2][1].y,
                bounds[3][0].x,bounds[3][0].y, bounds[3][1].x, bounds[3][1].y))
コード例 #6
0
import unionfind
from pprint import pprint
# Method to pretty print the nodes and what they point to
def pp():
    pprint(nodes, width=1)

E = ['a','b','c','d','e','f','g','h','i','j']
print('Nodes initialized to all equal null')
nodes = unionfind.init(E)
pp()

print('Union a and c, node a points to node c')
unionfind.union(nodes,'a','c')
pp()

print('Union b and d, Node b points to node d')
unionfind.union(nodes,'b','d')
pp()

print('Union c and f, node c points to node f')
unionfind.union(nodes,'c','f')
pp()

print('Union j with b, this points canonical rep of j to rep of b')
#node j points to node b, but
# b points to d, therefore j
# will point to d
unionfind.union(nodes,'j','b')
pp()

print('Union h with a, points canonical rep of h to rep of a')
コード例 #7
0
import unionfind
from pprint import pprint


# Method to pretty print the nodes and what they point to
def pp():
    pprint(nodes, width=1)


E = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
print('Nodes initialized to all equal null')
nodes = unionfind.init(E)
pp()

print('Union a and c, node a points to node c')
unionfind.union(nodes, 'a', 'c')
pp()

print('Union b and d, Node b points to node d')
unionfind.union(nodes, 'b', 'd')
pp()

print('Union c and f, node c points to node f')
unionfind.union(nodes, 'c', 'f')
pp()

print('Union j with b, this points canonical rep of j to rep of b')
#node j points to node b, but
# b points to d, therefore j
# will point to d
unionfind.union(nodes, 'j', 'b')