def __init__(self, file):
     self.file = file
     self.u = union_find()
     self.compressionu = union_find()
     self.all_nodes = []
     self.all_edges = []
     self.lines = self.file.readlines()
     self.c = Digraph('G', filename='chipgraph', format='dot')
def maximum_spacing_clustering(nodes, n_bits):
    # nodes: set {1,2,3....}.
    # connect all nodes pairs with distance <= 2
    # return number of clusters and a list of cluster members
    #def get_bit_value(num, position):
    #    # check if the n-th position of num is 1 (0-th position is the least significant digit)
    #    return (num & (1 << position)) > 0

    def set_value(num, position, value):
        # set the n-th position of num to the specified value.
        mask = 1 << position  # Compute mask, an integer with just bit 'index' set.
        num &= ~mask  # Clear the bit indicated by the mask
        if value:
            num |= mask  # If value was 1/True, set the bit indicated by the mask.
        return num

    def get_neighbors(num, n_bits):
        # generate "neighbors" that differ at at most [distance] positions of bit representation
        # return [set_value(num,i,0)+set_value(num,i,1)-num for i in range(n_bits)]
        tmp = [set_value(num, i, j) for i in range(n_bits) for j in (0, 1)]
        return [i for i in tmp if i != num]

    G = union_find.union_find(nodes)
    all_nodes = collections.defaultdict(list, {i: [i] for i in nodes})
    for num in nodes:
        neighbors = get_neighbors(num, n_bits)
        for nei in neighbors:
            if nei in all_nodes:
                for i in all_nodes[nei]:
                    G.union(num, i)
            all_nodes[nei].append(num)
    return G.print_disjoint_sets()
 def __init__(self, root, graph, comp):
     self.root = root
     self.graph = graph
     self.comp = comp
     self.all_nodes = self.comp.compressionu.get_encompassed_nodes(
         self.root)
     self.all_edges = []
     self.compressionu = union_find()
     self.c = Digraph('G', filename=str(self.root), format='dot')
def mst_kruskal(edges, nodes):  #
    heapq.heapify(edges)
    G = union_find.union_find(nodes)
    res = []
    for i in range(len(nodes) - 1):
        while 1:
            length, v, w = heapq.heappop(edges)
            if G.union(v, w):
                res.append((length, v, w))
                break
    return res
Exemple #5
0
def maximum_spacing_clustering(nodes, k, edges):
    # edges = [(len, tail, head), (len, tail, head), ...]
    # return: (max spacing, disjoint sets)
    G = union_find.union_find(nodes)
    if edges:
        edges.sort()
    for (l,t,h) in edges:
        if G.union(t,h):
            k+=1
        if k==len(nodes):
            disjoint_sets = G.print_disjoint_sets()
        if k==len(nodes)+1:
            return l, disjoint_sets
Exemple #6
0
def cluster(npar, all, target):
    x = union_find()
    count = 0
    length = len(npar)
    x.init_dataset(npar)
    while x.length() > target and count<length:
        x.put(npar[count])
        count += 1
    # calculate max-spacing and get rid of circles
    while count < length:
        if x.is_circle(npar[count,0], npar[count,1]):
            count += 1
        else:
            return npar[count, -1]
    return 0
Exemple #7
0
def cluster(npar, all, target):
    x = union_find()
    count = 0
    length = len(npar)
    x.init_dataset(npar)
    while x.length() > target and count < length:
        x.put(npar[count])
        count += 1
    # calculate max-spacing and get rid of circles
    while count < length:
        if x.is_circle(npar[count, 0], npar[count, 1]):
            count += 1
        else:
            return npar[count, -1]
    return 0
def kruskals_algorithm(graph):
    mst = adjacency_list.adjacency_list()
    uf = union_find.union_find(graph.get_vertices())
    pq = []
    for edge in graph.get_edges():
        heapq.heappush(pq, edge)

    while pq:
        weight, src, dst = heapq.heappop(pq)
        if not uf.same_component(src, dst):
            mst.add_vertex(src)
            mst.add_vertex(dst)
            mst.add_edge(src, dst, weight)
            uf.union(src, dst)
    return mst
Exemple #9
0
def min_spanning_tree(G):
    """ Minimum spanning tree using UF data structure and Kruskal algorithm """
    edges = []
    uf = union_find()
    for i in range(len(G)):
        for j in range(len(G[i])):
            edges.append((G[i][j], i, j))
    tree = []
    C = {}
    R = {}
    for u in range(len(G)):
        C[u] = u
    for u in range(len(G)):
        R[u] = 0
    for W, u, v in sorted(edges):
        if uf.find(C, u) != uf.find(C, v):
            tree.append((u, v))  # , set(self.max_cliques[u]).intersection(set(self.max_cliques[v]))))
            uf.union(C, R, u, v)
    return tree
    def kruskalls_maze(self):
        min_heap = []

        pair_added_to_heap = set([])
        # we need a min heap to keep track of which vertices will be added
        # the objects in the min heap will be in the order of weight, from_vertex, to_vertex
        for point in self.random_edge_weights:
            for connection in self.random_edge_weights.get(point):
                if (point, connection) not in pair_added_to_heap and (
                        connection, point) not in pair_added_to_heap:
                    heapq.heappush(
                        min_heap,
                        (self.random_edge_weights.get(point)[connection],
                         point, connection))
                    pair_added_to_heap.add((point, connection))

        uf = union_find(self.rows * self.columns)
        # added_to_tree = set([])

        while (uf.get_num_components() > 1):
            next_connection = heapq.heappop(min_heap)
            #since we kept a 1D array for the parents of our union_find object, we have to send
            # over the calculated index, which is just
            # (the number of columns) * (the vertex's row // 2) + (the vertex's column // 2)

            if not uf.are_in_same_component(
                    self.columns * (next_connection[1][0] // 2) +
                (next_connection[1][1] // 2),
                    self.columns * (next_connection[2][0] // 2) +
                (next_connection[2][1] // 2)):

                self.remove_wall(next_connection[1], next_connection[2])
                uf.union(
                    self.columns * (next_connection[1][0] // 2) +
                    next_connection[1][1] // 2,
                    self.columns * (next_connection[2][0] // 2) +
                    next_connection[2][1] // 2)
nodes = []

with open(infile, "r") as f:
    first_line = f.readline().split()
    node_num = int(first_line[0])
    bit_num = int(first_line[1])
    for line in f.readlines():
        #nodes.append([int(m) for m in line.split()])
        nodes.append(int(line.strip().replace(" ", ""), 2))

if test:
    print node_num
    print bit_num
    print nodes

nodes_union = union_find(node_num)

clusters = node_num

for n in range(1, node_num+1):
    all_neighbors = distance_less_than_3(nodes[n-1], bit_num)
    for m in range(n+1, node_num+1):
        if nodes_union.find(n) != nodes_union.find(m):
            if find_in_ascending_list(all_neighbors, nodes[m-1]):
                nodes_union.union(n, m)
                clusters -= 1

print "maximum k is %d" %clusters
f.close()

Exemple #12
0
def iterate(data):
    x = union_find()
    already = []
    [put_point(x, already, k, 3) for k in data]
    print x.length()
 def testUnionFind(self):
     list_of_tuples = [[0, 3], [0, 1], [2, 4], [4, 5], [1, 6], [1, 7],
                       [5, 8]]
     number_of_nodes = 10
     self.assertEqual(
         union_find.union_find(number_of_nodes, list_of_tuples), 3)
Exemple #14
0
# sort edges based on edge.cost
# if test:
#   for e in edges:
#       print e.__dict__

edges_sorted = sorted(edges, key=lambda e: e.cost)

#print edges_sorted[0].__dict__
#print edges_sorted[1].__dict__
if test:
   for e in edges_sorted:
       print e.__dict__


nodes = union_find(num)

cluster = num

for e in edges_sorted:
    if cluster > 4:
        if nodes.find(e.node1) != nodes.find(e.node2):
            if test:
                print "merging %d and %d" %(e.node1, e.node2)
                print "node %d's head is %d, node %d's head is %d" %(e.node1, nodes.find(e.node1), e.node2, nodes.find(e.node2))
            nodes.union(e.node1, e.node2)
            cluster -= 1
    else:
        if nodes.find(e.node1) != nodes.find(e.node2): 
            print "minimum spacing is %d" %e.cost
            break
Exemple #15
0
def iterate(data):
    x = union_find()
    already = []
    [put_point(x, already, k, 3) for k in data]
    print x.length()