示例#1
0
def k_means_clustering(k, num_nodes, edges):

    print edges
    U = UnionFind(num_nodes)
    X = []

    cluster_count = num_nodes
    for everyedge in edges:
        p = everyedge[1] - 1
        q = everyedge[2] - 1

        if cluster_count == k:
            break

        if U.root(p) != U.root(q):
            X.append(everyedge)
            U.union(p, q)
            cluster_count = cluster_count - 1

    print U
    i = 0
    k_min_spacing = float('inf')
    while (i < len(edges)):
        w, p, q = edges[i]
        if U.root(p - 1) != U.root(q - 1):
            print p, q, w
            k_min_spacing = w if w < k_min_spacing else k_min_spacing
        i = i + 1

    return k_min_spacing
示例#2
0
def k_means_cluster(edges, num_nodes, dist_bits):

    # print pprint.pformat(edges)
    U = UnionFind(num_nodes)
    cluster_count = num_nodes
    # cluster with hamming distance 0
    for everyedge in edges:
        hamming_0 = [everyedge]
        for e in hamming_0:
            try:
                for p in edges[everyedge]:
                    for q in edges[e]:
                        if U.root(p) != U.root(q):
                            U.union(p, q)
                            cluster_count = cluster_count - 1
            except:
                continue

        print "Hamming 0 done"
        hamming_1 = create_hamming_1(everyedge)
        for e in hamming_1:
            try:
                for p in edges[everyedge]:
                    for q in edges[e]:
                        # print p, q
                        if U.root(p) != U.root(q):
                            U.union(p, q)
                            cluster_count = cluster_count - 1
            except:
                continue
        print "Hamming 1 done"

        hamming_2 = create_hamming_2(everyedge)
        for e in hamming_2:
            try:
                for p in edges[everyedge]:
                    for q in edges[e]:
                        # print p, q
                        if U.root(p) != U.root(q):
                            U.union(p, q)
                            cluster_count = cluster_count - 1
            except:
                continue
        print "Hamming 2 done"

    return cluster_count