I cannot get what do I miss in my code, mostly right, but in graph that is large enough, WA appeared. graph is a dict which stores input and looks like:
graph = {
4: {5, 6, 7, 8, 9},
5: {4, 7, 9},
6: {4, 7},
7: {4, 5, 8, 9},
8: {4, 7, 9},
9: {4, 5, 7, 8, 9}
}
graph = take_inputs()
cliques = {}
for i in range(3, max(graph)+1):
cliques[i] = []
def bron_kerbosch_pivot(R, P, X, graph):
if not P and not X:
# Report R as a maximal clique
cliques[len(R)].append(R)
return
for v in P - graph[(P|X).pop()]:
bron_kerbosch_pivot(R | {v}, P & graph[v], X & graph[v], graph)
P.remove(v)
X.add(v)
def print_clique(cliques):
for length in cliques:
if not cliques[length]:
return
group = sorted(cliques[length], key=lambda x:sorted(x))
print(length)
print('\n'.join(map(lambda clique:('{' + ','.join(map(str, sorted(clique))) + '}'), group)))
bron_kerbosch_pivot(set(), set(graph), set(), graph)
print_clique(cliques)
It is my expected input, nothing wrong when input is that small
3
{4,6,7}
4
{4,5,7,9}
{4,7,8,9}