def bt(at,picked,seen): if upper_bound is not None and len(picked) > upper_bound: pass elif picked == set(patterns): pass elif at == len(patterns): yield picked else: for s in subsets(list(sub[at] - picked - seen)): npicked = set(picked) ok = True for add in set(s): if not can_add(add, npicked): ok = False break npicked.add(add) if not ok: continue found = False for q in npicked: if patterns[at].contains(q): found = True break if not found: continue for res in bt(at+1,npicked,seen | sub[at]): yield res
def bt(at, picked, seen): if upper_bound is not None and len(picked) > upper_bound: pass elif picked == set(patterns): pass elif at == len(patterns): yield picked else: for s in subsets(list(sub[at] - picked - seen)): npicked = set(picked) ok = True for add in set(s): if not can_add(add, npicked): ok = False break npicked.add(add) if not ok: continue found = False for q in npicked: if patterns[at].contains(q): found = True break if not found: continue for res in bt(at + 1, npicked, seen | sub[at]): yield res
def test_subsets(self): for lst in [[], [5,6,4], [1,2,3], [2,8], [5,9,1,1]]: it = subsets(lst) for i in range(1<<len(lst)): cur = next(it) self.assertEqual([ v for k,v in enumerate(lst) if (i & (1<<(len(lst)-k-1))) != 0 ], cur) with self.assertRaises(StopIteration): next(it)
def take_closure(self): it = 0 while True: it += 1 changed = False sys.stderr.write('Shading lemma closure (no %d)\n' % (it)) cnt = len(self.mps) ss = {} for i in range(cnt): ss.setdefault(self.uf.find(i),[]) ss[self.uf.find(i)].append(i) ProgressBar.create(len(ss)) for _,v in sorted(ss.items(),key=lambda k: min(k[1])): ProgressBar.progress() minima = [] maxima = [] for i in v: mp_cur = self.mps[i] is_maximal = True is_minimal = True for j in v: if i == j: continue mp = self.mps[j] if mp_cur.mesh <= mp.mesh: is_maximal = False if mp.mesh <= mp_cur.mesh: is_minimal = False if is_maximal: maxima.append(mp_cur) if is_minimal: minima.append(mp_cur) for mn in minima: for mx in maxima: if mn.mesh <= mx.mesh: for add in subsets(list(mx.mesh - mn.mesh)): mid = mn.shade(set(add)) if self.uf.unite(self.mps[mn], self.mps[mid]): changed = True ProgressBar.finish() if not changed: break
def supersets_of_mesh(n, mesh): left = [ (i,j) for i in range(n) for j in range(n) if (i,j) not in mesh ] for sub in subsets(left): yield (mesh | set(sub))
def supersets_of_mesh(n, mesh): left = [(i, j) for i in range(n) for j in range(n) if (i, j) not in mesh] for sub in subsets(left): yield (mesh | set(sub))