-
Notifications
You must be signed in to change notification settings - Fork 0
/
exactcover.py
71 lines (50 loc) · 1.7 KB
/
exactcover.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from random import shuffle
from collections import defaultdict
from collections.abc import Iterator
class ExactcoverSolver(Iterator):
def __init__(self, constrs, init=(), random=False):
self.random = random
self.constrs = constrs
self.choices = defaultdict(set)
for i in self.constrs:
for j in self.constrs[i]:
self.choices[j].add(i)
self.unsat = set(self.choices)
self.solu = []
try:
for i in init:
self.select_choice(i)
self.iter = self.solve()
except KeyError:
self.iter = iter(())
def select_choice(self, i):
self.solu.append(i)
for j in self.constrs[i]:
self.unsat.remove(j)
for k in self.choices[j]:
for l in self.constrs[k]:
if l != j:
self.choices[l].remove(k)
def unselect_choice(self, i):
last = self.solu.pop()
assert i == last
for j in self.constrs[i]:
self.unsat.add(j)
for k in self.choices[j]:
for l in self.constrs[k]:
if l != j:
self.choices[l].add(k)
def solve(self):
if not self.unsat:
yield list(self.solu)
return
best = min(self.unsat, key=lambda j:len(self.choices[j]))
choices = list(self.choices[best])
if self.random:
shuffle(choices)
for i in choices:
self.select_choice(i)
yield from self.solve()
self.unselect_choice(i)
def __next__(self):
return next(self.iter)