Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import defaultdict
- def find_triangles(graph):
- triangles = []
- for a in graph:
- for b in graph[a]:
- if a < b:
- for c in graph[b]:
- if c in graph[a] and b < c:
- triangles.append(tuple(sorted([a, b, c])))
- return list(filter(lambda i: any(j.startswith("t") for j in i), triangles))
- def algo(graph, r, potential, excluded, cliques):
- if not potential and not excluded:
- cliques.append(r)
- return
- pivot = next(iter(potential.union(excluded)))
- for v in potential - graph[pivot]:
- algo(
- graph,
- r.union([v]),
- potential.intersection(graph[v]),
- excluded.intersection(graph[v]),
- cliques
- )
- potential.remove(v)
- excluded.add(v)
- def find_max_clique(graph):
- cliques = []
- algo(graph, set(), set(graph.keys()), set(), cliques)
- return max(cliques, key=len)
- if __name__ == "__main__":
- with open("input.txt", "r") as f:
- data = f.read().splitlines()
- network = defaultdict(lambda: set())
- for i in data:
- a, b = i.split("-")
- network[a].add(b)
- network[b].add(a)
- print(len(find_triangles(network)))
- cliques = find_max_clique(network)
- print(','.join(sorted(cliques)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement