Пример #1
0
    primes = [a for a in primes if a >= P]
    print "calculating factors"
    set_dict2 = {}
    for prime in primes:
        set_dict2[prime] = [a for a in interval if a % prime == 0]
    #primes_dict = {item: calc_prime_factor(item, primes, P) for item in range(A, B+1)}
    #print primes_dict
    print "creating UnionFind"
    set_list = UnionFind()
    for item in range(A, B + 1):
        set_list.makeSet([item])
    #set_dict = {prime: [] for prime in primes}
    #print "creating sets"
    #for item in range(A,B+1):
    #    for prime in primes_dict[item]:
    #        set_dict[prime].append(item)
    #print set_dict
    #print set_list.getNumGroups()
    print "reducing sets"
    for item in set_dict2:
        temp_list = set_dict2[item]
        if len(temp_list) > 1:
            for new_item in temp_list:
                set_list.union(temp_list[0], new_item)
    results = set_list.getNumGroups()
    #print results
    #    print i, finish_time, farm_time, prev_comp_time, comp_time, cum_time, counter
    output.append("Case #" + str(case) + ": %i" % results)

write_output(output)
Пример #2
0
def clustering_big():
    rV=[]
    with open('clustering_big.txt') as f:
        s=f.readline()
        bits=int(s.split()[1])
        for line in f:
            s=line.replace(' ','')
            v=int(s,2)
            insort_left(rV,v)
    
    n=len(rV)
    
    # only collect vertices one of which has a distance less than 3 with another one.
    V=set([])
    G=[]

    # The brute-force way definitely runs in O (n^2) time. It's too slow.
    # Consider how many ways you can flip 1 bit in a node: 24.  How many ways can you flip 2 bits: 24*23/2.  
    # All together that's only 300 possibilities to try per node.
    ops=[1]
    for i in xrange(bits-1):
        ops.append(ops[i]<<1)
    
    def flip(b,i):
        'flip the ith bit of b'
        o=ops[i]
        return (b&o^o)|(b&(~o))
    
    u=0
    while u<n:
        x=rV[u]
        # handle the case that duplicates of x exist.
        v=bisect_right(rV,x)
        dups=xrange(u,v)
        for i in dups:
            for j in xrange(i+1,v):
                G.append((i,j))
                V.add(i)
                V.add(j)
        
        for j in xrange(bits):
            # handle the case of flipping 1 bit.
            y=flip(x,j)
            for v in bi_index_range(rV,y):
                for i in dups: # handle duplicates, no need to re-compute.
                    G.append((i,v))
                    V.add(i)
                V.add(v)
            # handle the case of flipping 2 bits. 
            for k in xrange(j+1,bits):
                z=flip(y,k)
                for v in bi_index_range(rV,z):
                    for i in dups: # handle duplicates, no need to re-compute.
                        G.append((i,v))
                        V.add(i)
                    V.add(v)
        
        u+=len(dups) # handle duplicates, no need to re-compute.
              
#     print rV
#     print G

    # compute how many clusters these 2-distance vertices union.
    unionfind=UnionFind()
    for u,v in G:
        # check cycle. No cycles if either vertex has not be added to any cluster or they belong to different clusters.
        if unionfind.find(u) is None or unionfind.find(v) is None or unionfind.find(u)!=unionfind.find(v):
            # add the edge.
            unionfind.union(u,v)
    
    return n-len(V)+unionfind.getNumGroups()
Пример #3
0
from UnionFind import UnionFind
import random

# build the graph 
with open('data/clustering_big.txt') as f: 
	nodes = {}
	clusters = UnionFind()
	for i, line in enumerate(f.readlines()): 
		if i == 0: 
			N, numbits = line.split()
			N, numbits = int(N), int(numbits) 
		else: 
			bits = line.strip().replace(" ", "")
			nodes[bits] = True
			clusters.makeSet([bits])

# run the algorithm
for k, node in enumerate(nodes.iterkeys()): 
	if k%1000 == 0: 
		print k
	for i in xrange(numbits): 
		alt = node[:i] + node[i].replace(node[i], str(1-int(node[i]))) + node[i+1:]
		if nodes.get(alt, False):
			clusters.union(node, alt) 
		for j in xrange(i+1, numbits): 
			alt2 = alt[:j] + alt[j].replace(alt[j], str(1-int(alt[j]))) + alt[j+1:]
			if nodes.get(alt2, False):
				clusters.union(node, alt2) 

print clusters.getNumGroups()
Пример #4
0
    set_dict2 = {}
    for prime in primes:
        set_dict2[prime] = [a for a in interval if a % prime == 0]
    #primes_dict = {item: calc_prime_factor(item, primes, P) for item in range(A, B+1)}
    #print primes_dict
    print "creating UnionFind"
    set_list = UnionFind()
    for item in range(A, B+1):
        set_list.makeSet([item])
    #set_dict = {prime: [] for prime in primes}
    #print "creating sets"
    #for item in range(A,B+1):
    #    for prime in primes_dict[item]:
    #        set_dict[prime].append(item)
    #print set_dict
    #print set_list.getNumGroups()
    print "reducing sets"
    for item in set_dict2:
        temp_list = set_dict2[item]
        if len(temp_list) > 1:
            for new_item in temp_list:
                set_list.union(temp_list[0],new_item)
    results = set_list.getNumGroups()
    #print results
    #    print i, finish_time, farm_time, prev_comp_time, comp_time, cum_time, counter
    output.append("Case #" + str(case) + ": %i" % results)

write_output(output)


Пример #5
0
def clustering_big():
    rV = []
    with open('clustering_big.txt') as f:
        s = f.readline()
        bits = int(s.split()[1])
        for line in f:
            s = line.replace(' ', '')
            v = int(s, 2)
            insort_left(rV, v)

    n = len(rV)

    # only collect vertices one of which has a distance less than 3 with another one.
    V = set([])
    G = []

    # The brute-force way definitely runs in O (n^2) time. It's too slow.
    # Consider how many ways you can flip 1 bit in a node: 24.  How many ways can you flip 2 bits: 24*23/2.
    # All together that's only 300 possibilities to try per node.
    ops = [1]
    for i in xrange(bits - 1):
        ops.append(ops[i] << 1)

    def flip(b, i):
        'flip the ith bit of b'
        o = ops[i]
        return (b & o ^ o) | (b & (~o))

    u = 0
    while u < n:
        x = rV[u]
        # handle the case that duplicates of x exist.
        v = bisect_right(rV, x)
        dups = xrange(u, v)
        for i in dups:
            for j in xrange(i + 1, v):
                G.append((i, j))
                V.add(i)
                V.add(j)

        for j in xrange(bits):
            # handle the case of flipping 1 bit.
            y = flip(x, j)
            for v in bi_index_range(rV, y):
                for i in dups:  # handle duplicates, no need to re-compute.
                    G.append((i, v))
                    V.add(i)
                V.add(v)
            # handle the case of flipping 2 bits.
            for k in xrange(j + 1, bits):
                z = flip(y, k)
                for v in bi_index_range(rV, z):
                    for i in dups:  # handle duplicates, no need to re-compute.
                        G.append((i, v))
                        V.add(i)
                    V.add(v)

        u += len(dups)  # handle duplicates, no need to re-compute.


#     print rV
#     print G

# compute how many clusters these 2-distance vertices union.
    unionfind = UnionFind()
    for u, v in G:
        # check cycle. No cycles if either vertex has not be added to any cluster or they belong to different clusters.
        if unionfind.find(u) is None or unionfind.find(
                v) is None or unionfind.find(u) != unionfind.find(v):
            # add the edge.
            unionfind.union(u, v)

    return n - len(V) + unionfind.getNumGroups()