def prune(g): numh = bfu.g2num(g) cannotprune = True for l in gk.edgelist(gk.digonly(g)): gk.delanedge(g,l) if bfu.forms_loop(g, loop): cannotprune = False prune(g) gk.addanedge(g,l) if cannotprune: s.add(bfu.g2num(g))
def prune(g): numh = bfu.g2num(g) cannotprune = True for l in sls.simple_loops(gk.digonly(g),0): gg = delAloop(g,bfu.g2num(loop2graph(l,n))) if bfu.forms_loop(gg, loop): cannotprune = False prune(gg) if cannotprune: print 'one' s.add(g)
def add2set_(ds, H, cp, ccf, iter=1, verbose=True, capsize=100): n = len(H) n2 = n * n + n dsr = {} s = set() ss = set() pbar = start_progress_bar(iter, len(ds), verbose=verbose) c = 0 for gnum in ds: g = bfu.num2CG(gnum, n) gnum = bfu.g2num(g) c += 1 pbar.update(c) glist = [] elist = [] eset = set() for e in ds[gnum]: if not e[1] in g[e[0]]: gk.addanedge(g, e) num = bfu.g2num(g) ekey = (1 << (n2 - int(e[0], 10) * n - int(e[1], 10))) if ekey in ccf and skip_conflictors(num, ccf[ekey]): gk.delanedge(g, e) continue if not num in s: if not bfu.call_u_conflicts(g, H): #cf, gl2 = bfu.call_u_conflicts2(g, H) #if not cf: glist.append((num, ekey)) elist.append(e) eset.add(ekey) s.add(num) if bfu.call_u_equals(g, H): ss.add(num) if capsize <= len(ss): break #if bfu.call_u_equals2(g, gl2, H): ss.add(num) gk.delanedge(g, e) for gn, e in glist: if e in cp: dsr[gn] = [ekey2e(k, n) for k in eset - cp[e]] else: dsr[gn] = elist if capsize <= len(ss): return dsr, ss pbar.finish() return dsr, ss
def add2set_(ds, H, cp, ccf, iter=1, verbose=True, capsize=100): n = len(H) n2 = n*n +n dsr = {} s = set() ss = set() pbar = start_progress_bar(iter, len(ds), verbose = verbose) c = 0 for gnum in ds: g = bfu.num2CG(gnum, n) gnum = bfu.g2num(g) c += 1 pbar.update(c) glist = [] elist = [] eset = set() for e in ds[gnum]: if not e[1] in g[e[0]]: gk.addanedge(g,e) num = bfu.g2num(g) ekey = (1<<(n2 - int(e[0],10)*n - int(e[1],10))) if ekey in ccf and skip_conflictors(num,ccf[ekey]): gk.delanedge(g,e) continue if not num in s: if not bfu.call_u_conflicts(g, H): #cf, gl2 = bfu.call_u_conflicts2(g, H) #if not cf: glist.append((num,ekey)) elist.append(e) eset.add(ekey) s.add(num) if bfu.call_u_equals(g, H): ss.add(num) if capsize <= len(ss): break #if bfu.call_u_equals2(g, gl2, H): ss.add(num) gk.delanedge(g,e) for gn,e in glist: if e in cp: dsr[gn] = [ekey2e(k,n) for k in eset - cp[e]] else: dsr[gn] = elist if capsize <= len(ss): return dsr, ss pbar.finish() return dsr, ss
def delAloop(g, loop): n = len(g) l = [] l = [bfu.g2num(ur.loop2graph(s,n)) for s in sls.simple_loops(g,0)] l = [num for num in l if not num == loop ] print loop, ': ', l return bfu.num2CG(reduce(operator.or_, l),n)
def allsloops(n, asl = alloops): if asl: return asl[n] s = [] l = gen_loops(n) for e in l: s.append(bfu.g2num(loop2graph(e,n))) return s
def simple_loops(g, u): """ iterator over the list of simple loops of graph g at the undersample rate u """ gx = graph2nx(num2CG(g2num(undersample(g, u)), len(g))) for l in networkx.simple_cycles(gx): yield l
def iteqclass(H, verbose=True, capsize=100): ''' Find all graphs in the same equivalence class with respect to graph H and any undesampling rate. ''' if cmp.isSclique(H): print 'not running on superclique' return None g = {n:{} for n in H} s = set() Hnum = bfu.ug2num(H) if Hnum[1]==0: s.add(Hnum[0]) cp = confpairs(H) ccf = conflictors(H) edges = gk.edgelist(gk.complement(g)) ds = {bfu.g2num(g): edges} if verbose: print '%3s'%'i'+'%10s'%' graphs' for i in range(len(H)**2): ds, ss = add2set_(ds, H, cp, ccf, iter=i, verbose=verbose, capsize=capsize) s = s | ss if capsize <= len(ss): break if not ds: break return s
def simple_loops(g, u): """ iterator over the list of simple loops of graph g at the undersample rate u """ gx = graph2nx(num2CG(g2num(undersample(g,u)), len(g))) for l in networkx.simple_cycles(gx): yield l
def clingo2g(output): s = set() answers = filterAnswers(output.split('\n')) answers = [a2edgetuple(x) for x in answers] l = [(c2edgepairs(x[0]),x[1]) for x in answers] l = [(bfu.g2num(edgepairs2g(x[0])),int(x[1][2:-1])) for x in l] return l
def clingo2g(output): s = set() answers = filterAnswers(output.split('\n')) answers = [a2edgetuple(x) for x in answers] l = [(c2edgepairs(x[0]), x[1]) for x in answers] l = [(bfu.g2num(edgepairs2g(x[0])), int(x[1][2:-1])) for x in l] return l
def add2set(gset, elist, H): n = len(H) s = set() ss = set() eremove = {e: True for e in elist} for gnum in gset: g = bfu.num2CG(gnum, n) for e in elist: if not e[1] in g[e[0]]: gk.addanedge(g,e) num = bfu.g2num(g) if not num in s: au = bfu.call_undersamples(g) if not gk.checkconflict(H, g, au=au): eremove[e] = False s.add(num) if gk.checkequality(H, g, au=au): ss.add(num) gk.delanedge(g,e) for e in eremove: if eremove[e]: elist.remove(e) return s, ss, elist
def iteqclass(H, verbose=True, capsize=100): ''' Find all graphs in the same equivalence class with respect to graph H and any undesampling rate. ''' if cmp.isSclique(H): print 'not running on superclique' return None g = {n: {} for n in H} s = set() Hnum = bfu.ug2num(H) if Hnum[1] == 0: s.add(Hnum[0]) cp = confpairs(H) ccf = conflictors(H) edges = gk.edgelist(gk.complement(g)) ds = {bfu.g2num(g): edges} if verbose: print '%3s' % 'i' + '%10s' % ' graphs' for i in range(len(H)**2): ds, ss = add2set_(ds, H, cp, ccf, iter=i, verbose=verbose, capsize=capsize) s = s | ss if capsize <= len(ss): break if not ds: break return s
def add2set(gset, elist, H): n = len(H) s = set() ss = set() eremove = {e: True for e in elist} for gnum in gset: g = bfu.num2CG(gnum, n) for e in elist: if not e[1] in g[e[0]]: gk.addanedge(g, e) num = bfu.g2num(g) if not num in s: au = bfu.call_undersamples(g) if not gk.checkconflict(H, g, au=au): eremove[e] = False s.add(num) if gk.checkequality(H, g, au=au): ss.add(num) gk.delanedge(g, e) for e in eremove: if eremove[e]: elist.remove(e) return s, ss, elist
def nodesearch(g, g2, order, inlist, s, cds, pool, pc): if order: if bfu.undersample(g, rate) == g2: s.add(bfu.g2num(g)) if capsize and len(s) > capsize: raise ValueError('Too many elements') s.update(supergraphs_in_eq(g, g2, rate=rate)) return g key = order[0] if pc: tocheck = [ x for x in pc if x in cds[len(inlist) - 1][inlist[0]] ] else: tocheck = cds[len(inlist) - 1][inlist[0]] if len(order) > 1: kk = order[1] pc = predictive_check(g, g2, pool[len(inlist)], c[edge_function_idx(kk)], kk) else: pc = set() adder, remover, masker = f[edge_function_idx(key)] checks_ok = c[edge_function_idx(key)] for n in tocheck: if not checks_ok(key, n, g, g2, rate=rate): continue masked = np.prod(masker(g, key, n)) if masked: nodesearch(g, g2, order[1:], [n] + inlist, s, cds, pool, pc) else: mask = adder(g, key, n) nodesearch(g, g2, order[1:], [n] + inlist, s, cds, pool, pc) remover(g, key, n, mask) elif bfu.undersample(g, rate) == g2: s.add(bfu.g2num(g)) if capsize and len(s) > capsize: raise ValueError('Too many elements') return g
def nodesearch0(g, g2, order, inlist, s, cds): if order: key = order.pop(0) tocheck = cds[len(inlist)-1][inlist[0]] adder, remover, masker = f[edge_function_idx(key)] checks_ok = c[edge_function_idx(key)] if len(tocheck) > 1: for n in tocheck: if not checks_ok(key,n,g,g2): continue mask = masker(g,key,n) if not np.prod(mask): mask = adder(g,key,n) r = nodesearch0(g,g2,order, [n]+inlist, s, cds) if r and bfu.increment(r)==g2: s.add(bfu.g2num(r)) if capsize and len(s)>capsize: raise ValueError('Too many elements') remover(g,key,n,mask) else: r = nodesearch0(g,g2,order, [n]+inlist, s, cds) if r and bfu.increment(r)==g2: s.add(bfu.g2num(r)) if capsize and len(s)>capsize: raise ValueError('Too many elements') elif tocheck: (n,) = tocheck mask = adder(g,key,n) r = nodesearch0(g,g2, order, [n]+inlist, s, cds) if r and bfu.increment(r) == g2: s.add(bfu.g2num(r)) if capsize and len(s)>capsize: raise ValueError('Too many elements') remover(g,key,n,mask) order.insert(0,key) else: return g
def addedges(g,H,edges): if edges: nedges = prune_conflicts(H, g, edges) n = len(nedges) if n == 0: return None for i in range(n): gk.addanedge(g,nedges[i]) if bfu.call_u_equals(g, H): s.add(bfu.g2num(g)) addedges(g,H,nedges[:i]+nedges[i+1:]) gk.delanedge(g,nedges[i])
def addedges(g, H, edges): if edges: nedges = prune_conflicts(H, g, edges) n = len(nedges) if n == 0: return None for i in range(n): gk.addanedge(g, nedges[i]) if bfu.call_u_equals(g, H): s.add(bfu.g2num(g)) s.add(addedges(g, H, nedges[:i] + nedges[i + 1:])) gk.delanedge(g, nedges[i])
def nodesearch(g, g2, order, inlist, s, cds, pool, pc): if order: if bfu.undersample(g,rate) == g2: s.add(bfu.g2num(g)) if capsize and len(s)>capsize: raise ValueError('Too many elements') s.update(supergraphs_in_eq(g, g2, rate=rate)) return g key = order[0] if pc: tocheck = [x for x in pc if x in cds[len(inlist)-1][inlist[0]]] else: tocheck = cds[len(inlist)-1][inlist[0]] if len(order) > 1: kk = order[1] pc = predictive_check(g,g2,pool[len(inlist)], c[edge_function_idx(kk)],kk) else: pc = set() adder, remover, masker = f[edge_function_idx(key)] checks_ok = c[edge_function_idx(key)] for n in tocheck: if not checks_ok(key,n,g,g2,rate=rate): continue masked = np.prod(masker(g,key,n)) if masked: nodesearch(g,g2,order[1:], [n]+inlist, s, cds, pool, pc) else: mask = adder(g,key,n) nodesearch(g,g2,order[1:], [n]+inlist, s, cds, pool, pc) remover(g,key,n,mask) elif bfu.undersample(g,rate)==g2: s.add(bfu.g2num(g)) if capsize and len(s)>capsize: raise ValueError('Too many elements') return g
def nodesearch(g, g2, edges, s): if edges: if bfu.undersample(g,rate) == g2: s.add(bfu.g2num(g)) if capsize and len(s)>capsize: raise ValueError('Too many elements') return g e = edges[0] for n in ln: if (n,e) in single_cache: continue if not ok2addaVpath(e, n, g, g2, rate=rate): continue mask = addaVpath(g,e,n) r = nodesearch(g,g2,edges[1:],s) delaVpath(g,e,n,mask) elif bfu.undersample(g,rate)==g2: s.add(bfu.g2num(g)) if capsize and len(s)>capsize: raise ValueError('Too many elements in eqclass') return g
def nodesearch(g, g2, edges, s): if edges: if bfu.increment(g) == g2: s.add(bfu.g2num(g)) if capsize and len(s)>capsize: raise ValueError('Too many elements') return g e = edges[0] for n in g2: if (n,e) in single_cache: continue if not edge_increment_ok(e[0],n,e[1],g,g2): continue mask = add2edges(g,e,n) r = nodesearch(g,g2,edges[1:],s) del2edges(g,e,n,mask) elif bfu.increment(g)==g2: s.add(bfu.g2num(g)) if capsize and len(s)>capsize: raise ValueError('Too many elements in eqclass') return g
def addnodes(g,g2,edges): if edges: masks = [] for e in edges: if ok2addanedge(e[0],e[1],g,g2,rate=rate): masks.append(True) else: masks.append(False) nedges = [edges[i] for i in range(len(edges)) if masks[i]] n = len(nedges) if n: for i in range(n): mask = addanedge(g,nedges[i]) s.add(bfu.g2num(g)) addnodes(g,g2,nedges[:i]+nedges[i+1:]) delanedge(g,nedges[i],mask)
def nodesearch(g, g2, edges, s): if edges: e = edges.pop() ln = [n for n in g2] for n in ln: if (n,e) in single_cache: continue mask = add2edges(g,e,n) if isedgesubsetD(bfu.increment(g), g2): r = nodesearch(g,g2,edges,s) if r and edgeset(bfu.increment(r))==edgeset(g2): s.add(bfu.g2num(r)) if capsize and len(s)>capsize: raise ValueError('Too many elements in eqclass') del2edges(g,e,n,mask) edges.append(e) else: return g
def eqclass_list(H): ''' Find all graphs in the same equivalence class with respect to graph H and any undesampling rate. ''' g = {n:{} for n in H} s = set() edges = gk.edgelist(gk.complement(g)) #edges = prune_conflicts(H, g, edges) gset = set([bfu.g2num(g)]) for i in range(len(H)**2): print i gset, ss, edges = add2set(gset, edges, H) s = s | ss if not edges: break return s
def nodesearch(g, g2, edges, s): if edges: #key, checklist = edges.popitem() key = random.choice(edges.keys()) checklist = edges.pop(key) adder, remover = f[edge_function_idx(key)] checks_ok = c[edge_function_idx(key)] for n in checklist: mask = adder(g,key,n) if isedgesubset(bfu.increment(g), g2): r = nodesearch(g,g2,edges,s) if r and bfu.increment(r)==g2: s.add(bfu.g2num(r)) if capsize and len(s)>capsize: raise ValueError('Too many elements') remover(g,key,n,mask) edges[key] = checklist else: return g
def eqclass_list(H): ''' Find all graphs in the same equivalence class with respect to graph H and any undesampling rate. ''' g = {n: {} for n in H} s = set() edges = gk.edgelist(gk.complement(g)) #edges = prune_conflicts(H, g, edges) gset = set([bfu.g2num(g)]) for i in range(len(H)**2): print i gset, ss, edges = add2set(gset, edges, H) s = s | ss if not edges: break return s
def reverse(H, verbose=True, capsize=1000): n = len(H) s = set() g = gk.superclique(n) sloops = set(allsloops(n)) ds = {bfu.g2num(g): sloops} if verbose: print '%3s'%'i'+'%10s'%' graphs' i=0 while ds: ds, ss = del_loop(ds, H, iter=i, verbose=verbose, capsize=capsize) s = s | ss i += 1 if capsize <= len(s): break return s
def gsig(g): ''' turns input graph g into a hash string using edges ''' return bfu.g2num(g)
def permuteAset(s, perm): n = len(perm) ns = set() for e in s: ns.add(bfu.g2num(permute(bfu.num2CG(e,n),perm))) return ns