Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2024
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. from collections import defaultdict
  2.  
  3. def find_triangles(graph):
  4. triangles = []
  5. for a in graph:
  6. for b in graph[a]:
  7. if a < b:
  8. for c in graph[b]:
  9. if c in graph[a] and b < c:
  10. triangles.append(tuple(sorted([a, b, c])))
  11.  
  12. return list(filter(lambda i: any(j.startswith("t") for j in i), triangles))
  13.  
  14. def algo(graph, r, potential, excluded, cliques):
  15. if not potential and not excluded:
  16. cliques.append(r)
  17. return
  18. pivot = next(iter(potential.union(excluded)))
  19. for v in potential - graph[pivot]:
  20. algo(
  21. graph,
  22. r.union([v]),
  23. potential.intersection(graph[v]),
  24. excluded.intersection(graph[v]),
  25. cliques
  26. )
  27. potential.remove(v)
  28. excluded.add(v)
  29.  
  30. def find_max_clique(graph):
  31. cliques = []
  32. algo(graph, set(), set(graph.keys()), set(), cliques)
  33. return max(cliques, key=len)
  34.  
  35. if __name__ == "__main__":
  36. with open("input.txt", "r") as f:
  37. data = f.read().splitlines()
  38.  
  39. network = defaultdict(lambda: set())
  40. for i in data:
  41. a, b = i.split("-")
  42. network[a].add(b)
  43. network[b].add(a)
  44.  
  45. print(len(find_triangles(network)))
  46. cliques = find_max_clique(network)
  47. print(','.join(sorted(cliques)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement