Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def day23_visualize(s):
- graph = networkx.Graph()
- graph.add_edges_from(line.split('-') for line in s.splitlines())
- def get_triangles(graph):
- for a in graph:
- for b in graph.neighbors(a):
- if b > a:
- for c in graph.neighbors(a):
- if c > b and graph.has_edge(b, c):
- yield a, b, c
- triangles = get_triangles(graph)
- sought_triangles = [t for t in triangles if any(node[0] == 't' for node in t)]
- largest_clique = max(networkx.find_cliques(graph), key=len)
- pos = networkx.nx_agraph.graphviz_layout(graph, prog='neato', args='-Gstart=1') # Requires pygraphviz.
- plot_args: Any = dict(figsize=(12, 12), dpi=80)
- draw_args = dict(node_size=60, width=0.3)
- if 1: # First plot.
- fig, ax = plt.subplots(**plot_args)
- ax.set_aspect('equal')
- node_color = [('red' if node[0] == 't' else 'C0') for node in graph]
- networkx.draw(graph, pos, node_color=node_color, **draw_args)
- # Draw triangles.
- for triangle in sought_triangles:
- vertices = [pos[node] for node in triangle]
- # Create and add a semi-transparent triangle.
- poly = matplotlib.patches.Polygon(vertices, alpha=0.03, facecolor='green', edgecolor='none')
- ax.add_patch(poly)
- fig.tight_layout(pad=0)
- if 1: # Second plot.
- fig, ax = plt.subplots(**plot_args)
- ax.set_aspect('equal')
- node_color = [('red' if node in largest_clique else 'C0') for node in graph]
- networkx.draw(graph, pos, node_color=node_color, **draw_args)
- fig.tight_layout(pad=0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement