def ctrl_transpose(self): grapht = transpose(self._G) if not self._G.is_graphb(): fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(25,15)) ax = axes.flatten() graph = self._G # print(graph.is_valued()) PRINT_GRAPH(graph, ax[0], 'Original') grapht = transpose(graph) PRINT_GRAPH(grapht, ax[1], 'Transposto') # print(graph.vertexes['b']) plt.show() # display else: print('A entrada deve ser um dígrafo.') input()
def STRONGLY_CONNECTED_COMPONENTS(G: Graph): order = TOPOLOGICAL_SORT(G) Gt = transpose(G) order.reverse() # print(order) fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(25, 15)) # ax = axes.flatten() # print(sorted(G.vertexes)) res = DFS(Gt, all=True, order=order) random.seed() roots = set() for v in res.r: roots.add(res.r[v]) hex_colors = {} r = lambda: random.randint(0, 255) for i in roots: hex_colors[i] = '#%02X%02X%02X' % (r(), r(), r()) # print(hex_colors) # print(res.r) colors = {'roots': roots, 'hex': hex_colors, 'elements': res.r} PRINT_GRAPH(G, axes, 'Componentes Fortemente Conexas', colors=colors, colors_fun=get_colors_components) # PRINT_GRAPH(Gt, ax[1], 'Transposto') plt.show() # display return roots, res.r
def ctrl_BFS(self, s): graph = self._G fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(25, 15)) PRINT_GRAPH(graph, axes[0], "Grafo", colors=None, colors_fun=get_colors_tree) result = BFS(graph, s) edges= [] order = list(result.q_path.queue) # print(order) # input(graph.get_type()) if(graph.get_type() == Type.DIGRAPH): graph_caminho = AdjMatrix(graph.get_len(), Type.DIGRAPH, False) else: graph_caminho = AdjMatrix(graph.get_len(), Type.GRAPH, False) ant = order.pop(0) for v in order: edges.append((ant, v)) graph_caminho.make_relation(ant, v) ant = v PRINT_GRAPH(graph_caminho, axes[1], "BFS", colors=edges, colors_fun=get_colors_bfs) plt.show() # display
def ctrl_topological_sort(self): graph = self._G if not self._G.is_graphb(): fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(25, 15)) PRINT_GRAPH(graph, axes[0], "Grafo", colors=None, colors_fun=get_colors_tree) order = TOPOLOGICAL_SORT(graph) edges= [] order.reverse() # print(order) graph_caminho = AdjMatrix(graph.get_len(), Type.DIGRAPH, False) ant = order.pop(0) for v in order: edges.append((ant, v)) graph_caminho.make_relation(ant, v) ant = v PRINT_GRAPH(graph_caminho, axes[1], "Ordem Topológica", colors=edges, colors_fun=get_colors_bfs) plt.show() # display else: print('A entrada deve ser um dígrafo.') input()
def ctrl_mst(self): graph = self._G if self._G.is_graphb(): result = KRUSKAL(graph) graph = self._G fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(25, 15)) # print(result) soma = 0 for val in result: soma+=val[2] PRINT_GRAPH(graph, axes, "AGM - Kruskal - Custo Total: {}".format(soma), colors=result, colors_fun=get_colors_tree) plt.show() # display else: print('A entrada deve ser um grafo.') input()
def ctrl_shortest_path(self, s): graph = self._G graph.set_num_edges(self.get_len_edges(self._contents)) result = DIJKSTRA(graph, s) edges = [] textstr = 'Distâncias Mínimas:' for v in graph.vertexes: if v != s: soma = 0 path = result.get_path(v) for u, w in path: soma += graph.get_value(u, w) textstr += "\n{}: {}".format(v, soma) edges += path # print(soma) fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(25, 15)) props = dict(boxstyle='round', facecolor='wheat', alpha=0.5) axes.text(0.05, 0.95, textstr, transform=axes.transAxes, fontsize=14, verticalalignment='top', bbox=props) PRINT_GRAPH(graph, axes, "Caminho Mínimo - Dijkstra - Início({})".format(s), colors=edges, colors_fun=get_colors_dij) plt.show() # display
def connected_components(G: Graph): res = DFS(G) random.seed() r = lambda: random.randint(0, 255) fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(25, 15)) roots = set() for v in res.r: roots.add(res.r[v]) hex_colors = {} for i in roots: hex_colors[i] = '#%02X%02X%02X' % (r(), r(), r()) colors = {'roots': roots, 'hex': hex_colors, 'elements': res.r} PRINT_GRAPH(G, axes, 'Componentes Conexas', colors=colors, colors_fun=get_colors_components) plt.show() return roots, res.r
# elif(len(x) == 2): # graph.make_relation(x[0], x[1]) # else: # graph.make_relation(x[0], x[1], int(x[2])) # # graph.print_all() # return graph def main(): tests() if __name__ == "__main__": contents = read_file("test/test_files/15_kruskal.txt") graph = generate_graph(contents, True, False, True) result = KRUSKAL(graph) fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(25, 15)) print(result) soma = 0 for val in result: soma += val[2] PRINT_GRAPH(graph, axes, "AGM - Kruskal - Custo Total: {}".format(soma), colors=result, colors_fun=get_colors_tree) plt.show() # display # main()
graph.set_num_edges(get_len_edges(contents)) start = 't' result = DIJKSTRA(graph, start) edges = [] textstr = 'Distâncias Mínimas:' for v in graph.vertexes: if v != start: soma = 0 path = result.get_path(v) for u, w in path: soma += graph.get_value(u, w) textstr += "\n{}: {}".format(v, soma) edges += path print(soma) fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(25, 15)) props = dict(boxstyle='round', facecolor='wheat', alpha=0.5) axes.text(0.05, 0.95, textstr, transform=axes.transAxes, fontsize=14, verticalalignment='top', bbox=props) PRINT_GRAPH(graph, axes, "Caminho Mínimo - Dijkstra - Início({})".format(start), colors=edges, colors_fun=get_colors_dij) plt.show() # display
# return graph def main(): # tests() pass if __name__ == "__main__": # main() contents = read_file("test/test_files/16_BFS.txt") graph = generate_graph(contents, False, False, False) fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(25, 15)) PRINT_GRAPH(graph, axes[0], "Grafo", colors=None, colors_fun=get_colors_tree) result = BFS(graph, 'F') edges = [] order = list(result.q_path.queue) # print(order) graph_caminho = AdjMatrix(graph.get_len(), Type.DIGRAPH, False) ant = order.pop(0) for v in order: edges.append((ant, v)) graph_caminho.make_relation(ant, v) ant = v PRINT_GRAPH(graph_caminho,
def coloring(G: Graph): if G.get_len() == 0: return None aux = ColoringAux(G) if G.get_len() == 1: for i in G.vertexes: aux.colored[i] = aux.cc else: aux = ColoringAux(G) u = bigger_degree(G) coloring_vertex(G, u, aux) # input(G.vertexes) for v in G.vertexes: if v not in aux.colored.keys(): coloring_vertex(G, v, aux) hex_colors = {} # print(aux.colored.keys()) # input() cor = get_spaced_colors(aux.cc + 1) #cria as cores na quantidade cromática # print(cor) # input(len(cor)) # print(aux.colored) # exit() # flipped = {} # for key, value in aux.colored.items(): # if value not in flipped: # flipped[value] = [key] # else: # flipped[value].append(key) # print(flipped) # print(len(flipped)) # i = 0 # j = 1 # print(cor) # print(len(cor)) # input() # cor = [(0, 255, 0), (255, 0, 0), (0, 0, 255)] # while i < len(flipped): # for x in flipped[i]: # hex_colors[x] = '#%02X%02X%02X' % cor[i] # # print(cor[i]) # i+=1 for i in aux.colored: hex_colors[i] = '#%02X%02X%02X' % cor[aux.colored[i]] # print(hex_colors) # hex_colors[i] = '#%02X%02X%02X' % (aux.colored[i]*89, aux.colored[i]*12, aux.colored[i]*100) # print(cor) colors = {'hex': hex_colors} # print(colors) # input() plt.figure(figsize=(25, 15)) plt.axis('off') PRINT_GRAPH(G, colors=colors, colors_fun=get_colors_coloring) plt.title('Valor cromático: {0}'.format(aux.cc + 1)) plt.show() return aux
from test.helper_leitura import * from algorithms.PRINT_GRAPH import * from algorithms.MINIMUM_PATH_AOG import * from structs.graph import Type if __name__ == "__main__": # main() contents = read_file("test/test_files/17_topological.txt") graph = generate_graph(contents, False, False, False) fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(25, 15)) PRINT_GRAPH(graph, axes[0], "Grafo", colors=None, colors_fun=get_colors_tree) order = TOPOLOGICAL_SORT(graph) edges = [] order.reverse() # print(order) graph_caminho = AdjMatrix(graph.get_len(), Type.DIGRAPH, False) ant = order.pop(0) for v in order: edges.append((ant, v)) graph_caminho.make_relation(ant, v) ant = v PRINT_GRAPH(graph_caminho, axes[1], "Ordem Topológica", colors=edges,
from algorithms.TRANSPOSE import * from algorithms.PRINT_GRAPH import * from test.helper_leitura import * contents = read_file("test/test_files/11_prim_slide.txt") fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(25, 15)) ax = axes.flatten() graph = generate_graph(contents, True, False, False) print(graph.is_valued()) PRINT_GRAPH(graph, ax[0], 'Original') grapht = transpose(graph) PRINT_GRAPH(grapht, ax[1], 'Transposto') print(graph.vertexes['b']) plt.show() # display