当前位置: 首页>>代码示例>>Python>>正文


Python UnionFind.getNumGroups方法代码示例

本文整理汇总了Python中UnionFind.UnionFind.getNumGroups方法的典型用法代码示例。如果您正苦于以下问题:Python UnionFind.getNumGroups方法的具体用法?Python UnionFind.getNumGroups怎么用?Python UnionFind.getNumGroups使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在UnionFind.UnionFind的用法示例。


在下文中一共展示了UnionFind.getNumGroups方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: calc_prime_factor

# 需要导入模块: from UnionFind import UnionFind [as 别名]
# 或者: from UnionFind.UnionFind import getNumGroups [as 别名]
    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)


开发者ID:agileminor,项目名称:shared_files,代码行数:30,代码来源:number_set.py

示例2: clustering_big

# 需要导入模块: from UnionFind import UnionFind [as 别名]
# 或者: from UnionFind.UnionFind import getNumGroups [as 别名]
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()
开发者ID:BurningXFlame,项目名称:SADA_Assignments,代码行数:73,代码来源:clustering.py

示例3: open

# 需要导入模块: from UnionFind import UnionFind [as 别名]
# 或者: from UnionFind.UnionFind import getNumGroups [as 别名]
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()
开发者ID:njsm87,项目名称:algorithms,代码行数:32,代码来源:clustering_big-4.py


注:本文中的UnionFind.UnionFind.getNumGroups方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。