def generate_image(graph: pydot.Dot, red_nodes: List[str]) -> str: for node in red_nodes: node = graph.get_node(node)[0] node.set_color("#d35d6e") _, png = mkstemp() with open(png, "wb") as f: f.write(graph.create_png()) print(f"Wrote intermediate step to {png}") return png
class Grafo: def __init__(self, vertices, use_pydot=False): self.__verts__ = vertices self.__adjmx__ = [[] for i in range(vertices)] self.__arest__ = 0 if use_pydot: self.__graph__ = Dot(graph_type='graph') for i in range(vertices): self.__graph__.add_node(Node(str(i))) else: self.__graph__ = None def aresta(self, v1, v2): if v2 in self.__adjmx__[v1]: return False self.__adjmx__[v1].append(v2) self.__adjmx__[v2].append(v1) self.__arest__ = self.__arest__ + 1 if self.__graph__ is not None: self.__graph__.add_edge(Edge(str(v1), str(v2))) return True def aresta_aleat(self): done = False while not done: v1 = __random_gen__.randint(self.__verts__) v2 = v1 while v2 == v1: v2 = __random_gen__.randint(self.__verts__) done = self.aresta(v1, v2) def conecta(self, alfa): max_art = (self.__verts__ * (self.__verts__ - 1)) // 2 arestas = round(alfa * max_art) for _ in range(arestas): self.aresta_aleat() def show(self): if self.__graph__ is not None: from IPython.display import Image return Image(self.__graph__.create(format='png')) def contemC4(self): for v in range(self.__verts__): tem_caminho = [False] * self.__verts__ for w in self.__adjmx__[v]: for u in self.__adjmx__[w]: if u != v: if tem_caminho[u]: return v else: tem_caminho[u] = True return -1 def testeC4(self, vert): def mostra_C4(v): if self.__graph__ is not None: for i in range(len(v)): self.__graph__.get_node(str(v[i]))[0].set_style('filled') self.__graph__.get_edge(str(v[i]), str( v[(i + 1) % len(v)]))[0].set_style('bold') for v in self.__adjmx__[vert]: for w in self.__adjmx__[v]: if w != vert: for u in self.__adjmx__[w]: if u != vert and u != v: for z in self.__adjmx__[u]: if z == vert: mostra_C4([vert, v, w, u]) return True return False