diff --git a/d23/part2_bron_kerbosch.py b/d23/part2_bron_kerbosch.py new file mode 100644 index 0000000..4567de6 --- /dev/null +++ b/d23/part2_bron_kerbosch.py @@ -0,0 +1,23 @@ +from collections import defaultdict as DD +import sys + +G = DD(set) + +for line in sys.stdin.read().strip().split('\n'): + a, b = line.split('-') + G[a].add(b) + G[b].add(a) + +def bron_kerbosch(r, p, x): + if len(p) == 0 and len(x) == 0: + yield r + else: + pivot = max(p | x, key=lambda x: len(G[x])) + for v in p - G[pivot]: + neighbours = G[v] + yield from bron_kerbosch(r | {v}, p & neighbours, x & neighbours) + p.remove(v) + x.add(v) + +clique = max(bron_kerbosch(set(), set(G.keys()), set()), key=len) +print(','.join(sorted(clique))) diff --git a/d23/part2_naive.py b/d23/part2_naive.py index 6d7ad38..741d984 100644 --- a/d23/part2_naive.py +++ b/d23/part2_naive.py @@ -1,7 +1,6 @@ import sys from collections import defaultdict as DD - D = DD(set) S = set() for i in sys.stdin.read().strip().split('\n'):