def graphDSD(database: str): metadata_db, index = get_metadata_db(database) list_aux = list() if metadata_db: table_dic: dict = metadata_db.get_tab() if len(table_dic) != 0: grafo = Graph() for key, table in table_dic.items(): list_fk: list = table.fk.extractForeign() if len(list_fk) != 0: for data in list_fk: table_1 = data[1] table_2 = data[3] if table_1 not in list_aux: grafo.add_vertex(str(table_1)) if table_2 not in list_aux: grafo.add_vertex(str(table_2)) grafo.join(str(table_1), str(table_2)) # print(f"{str(table_1)},{str(table_2)}") gra = grafo.graficar() ruta = f"graphDSD_{database}" generate_grapviz(gra, str(ruta)) else: return "Tables empty" else: return 1
def start(self): prim_graph = Graph("n") nodes_visited = list() nodes_to_visit = self.graph.nodes prim_graph.addNodes(nodes_to_visit) print(nodes_to_visit) nodes_to_visit["A"].data["key"] = 0 while nodes_to_visit: min = self.return_min_value(nodes_to_visit) nodes_to_visit.pop(min.name) print(nodes_to_visit) if min.data["parent"] is not None: # self.lista[min.name] = min.data["parent"] prim_graph.addEdge(min.name, min.data["parent"].name, min.data["key"]) nodes_adj = self.return_all_vertices(min) for node in nodes_adj: if node[1].weight < node[0].data["key"]: node[0].data["parent"] = min node[0].data["key"] = node[1].weight # break self.graph = prim_graph
def main(): G = Graph(is_directional=True) print("\n\n") print("Loading graph...") with open('following_followers', 'r') as fp: count_line = 0 user = [] for line in fp: user.append(line) count_line += 1 if count_line == 5: count_line = 0 username = user[0].strip('\n') name = user[1].strip('\n') avatar = user[1].strip('\n') followers = list(user[3].strip('\n').split(',')) for follower in followers: if follower == 'None': break G.add_vertex([(follower, username)]) following = list(user[4].strip('\n').split(',')) for user_following in following: if user_following == 'None': break G.add_vertex([(username, user_following)]) user = [] print("Graph loaded!") print("\n==========================================\n") print("Edges count: ", G.edges_count) print("Order: ", G.get_order()) print("\n==========================================\n") print("\n\n") print("Ctrl + C to quit") while True: try: user_1 = input('User 1: ') user_2 = input('User 2: ') print("\n\n") if G.get_vertex(user_1): if G.get_vertex(user_2): print("Separation degree: ", G.breadth_first_search(user_1, user_2)) else: print("User 2 '{}' is'n in this graph :<".format(user_2)) else: print("User 1 '{}' is'n in this graph :<".format(user_1)) if not G.get_vertex(user_2): print("User 2 '{}' is'n in this graph :<".format(user_2)) print("\n==========================================\n\n") except KeyboardInterrupt as e: print("\n\n") out = input('Do you really want to quit? [Y/n]: ') if out != 'n' and out != 'no' and out != 'N' and out != 'NO' and out != 'No' and out != 'nO': exit(0)
class GrafoTeste(unittest.TestCase): def setUp(self): self.grafo = Graph() def adicionar_vertices(self): a = Vertice('a') b = Vertice('b') c = Vertice('c') conj = {'a': a, 'b': b, 'c': c} for key in conj: self.grafo.adicionar_vertice(conj[key]) return conj def teste_adicionar_vertice_aumenta_tamanho(self): self.grafo.adicionar_vertice(Vertice('a')) self.assertEqual(self.grafo.ordem(), 1) def teste_remover_vertice_diminui_tamanho(self): a = Vertice('a') self.grafo.adicionar_vertice(a) self.grafo.remover_vertice(a) self.assertEqual(self.grafo.ordem(), 0) def teste_remover_aresta_inexistente(self): a = Vertice('a') b = Vertice('b') self.grafo.adicionar_vertice(a) self.grafo.adicionar_vertice(b) self.assertRaises(ArestaNaoExisteException, self.grafo.desconectar, a, b) def teste_vertice_nao_pode_ser_conectado_com_ele_mesmo(self): a = Vertice('a') self.grafo.adicionar_vertice(a) self.assertRaises(VerticeNaoPodeSeConectarComEleMesmoException, self.grafo.conectar, a,a) def teste_remover_vertice_remove_arestas_conectadas_nele(self): conj = self.adicionar_vertices() self.grafo.conectar(conj['a'], conj['b']) self.grafo.conectar(conj['b'], conj['c']) self.grafo.remover_vertice(conj['a']) self.assertFalse(self.grafo.estao_conectados(conj['a'], conj['b'])) self.assertTrue(self.grafo.estao_conectados(conj['b'], conj['c'])) def teste_conectar_dois_vertices(self): a = Vertice('a') b = Vertice('b') self.grafo.adicionar_vertice(a) self.grafo.adicionar_vertice(b) self.grafo.conectar(a, b) self.assertTrue(self.grafo.estao_conectados(a, b)) def teste_ordem(self): self.adicionar_vertices() self.assertEqual(self.grafo.ordem(), 3) def teste_obter_todos_os_vertices(self): conj = self.adicionar_vertices() self.assertDictEqual(self.grafo.vertices(), conj) def teste_adjacentes(self): conj = self.adicionar_vertices() a = conj['a'] b = conj['b'] c = conj['c'] self.grafo.conectar(a, b) self.grafo.conectar(a, c) self.assertSetEqual(self.grafo.adjacentes(a), {b, c}) def teste_grau_de_vertice(self): conj = self.adicionar_vertices() a = conj['a'] b = conj['b'] c = conj['c'] self.grafo.conectar(a, b) self.grafo.conectar(a, c) self.assertEqual(self.grafo.grau(a), 2) def teste_grafo_vazio_nao_e_regular(self): grafo = Graph() self.assertFalse(grafo.e_regular()) def teste_nao_regular(self): conj = self.adicionar_vertices() self.grafo.conectar(conj['a'], conj['b']) self.grafo.conectar(conj['a'], conj['c']) self.assertFalse(self.grafo.e_regular()) def teste_e_regular(self): conj = self.adicionar_vertices() self.grafo.conectar(conj['a'], conj['b']) self.grafo.conectar(conj['a'], conj['c']) self.grafo.conectar(conj['b'], conj['c']) self.assertTrue(self.grafo.e_regular()) def teste_e_completo(self): conj = self.adicionar_vertices() for v1 in conj: for v2 in conj: if v1 != v2: self.grafo.conectar(conj[v1], conj[v2]) self.assertTrue(self.grafo.e_completo()) def teste_nao_completo(self): conj = self.adicionar_vertices() for v1 in conj: for v2 in conj: if v1 != v2: self.grafo.conectar(conj[v1], conj[v2]) self.grafo.desconectar(conj['a'], conj['b']) self.assertFalse(self.grafo.e_completo()) def teste_fecho_transitivo_com_ciclo(self): a = Vertice('a') b = Vertice('b') c = Vertice('c') d = Vertice('d') self.grafo.adicionar_vertice(a) self.grafo.adicionar_vertice(b) self.grafo.adicionar_vertice(c) self.grafo.adicionar_vertice(d) self.grafo.adicionar_vertice(Vertice('e')) self.grafo.adicionar_vertice(Vertice('f')) self.grafo.conectar(a, b) self.grafo.conectar(a, c) self.grafo.conectar(c, b) self.grafo.conectar(c, d) self.grafo.conectar(Vertice('e'), Vertice('f')) self.assertSetEqual(self.grafo.fecho_transitivo(a), {a, b, c, d}) def teste_fecho_transitivo_sem_ciclo(self): a = Vertice('a') b = Vertice('b') c = Vertice('c') d = Vertice('d') self.grafo.adicionar_vertice(a) self.grafo.adicionar_vertice(b) self.grafo.adicionar_vertice(c) self.grafo.adicionar_vertice(d) self.grafo.adicionar_vertice(Vertice('e')) self.grafo.adicionar_vertice(Vertice('f')) self.grafo.conectar(a, b) self.grafo.conectar(a, c) self.grafo.conectar(c, d) self.grafo.conectar(Vertice('e'), Vertice('f')) self.assertSetEqual(self.grafo.fecho_transitivo(a), {a, b, c, d}) def teste_e_conexo(self): a = Vertice('a') b = Vertice('b') c = Vertice('c') d = Vertice('d') for vertice in {a, b, c, d}: self.grafo.adicionar_vertice(vertice) self.grafo.conectar(a, b) self.grafo.conectar(a, c) self.grafo.conectar(c, b) self.grafo.conectar(c, d) self.assertTrue(self.grafo.e_conexo()) def teste_nao_e_conexo(self): a = Vertice('a') b = Vertice('b') c = Vertice('c') d = Vertice('d') e = Vertice('e') for vertice in {a, b, c, d, e}: self.grafo.adicionar_vertice(vertice) self.grafo.conectar(a, b) self.grafo.conectar(a, c) self.grafo.conectar(c, b) self.grafo.conectar(c, d) self.assertFalse(self.grafo.e_conexo()) def teste_e_arvore(self): a = Vertice('a') b = Vertice('b') c = Vertice('c') d = Vertice('d') e = Vertice('e') f = Vertice('f') for vertice in {a, b, c, d, e, f}: self.grafo.adicionar_vertice(vertice) self.grafo.conectar(a, b) self.grafo.conectar(a, d) self.grafo.conectar(d, c) self.grafo.conectar(d, f) self.grafo.conectar(c, e) self.assertTrue(self.grafo.e_arvore()) def teste_nao_e_arvore(self): a = Vertice('a') b = Vertice('b') c = Vertice('c') d = Vertice('d') e = Vertice('e') f = Vertice('f') for vertice in {a, b, c, d, e, f}: self.grafo.adicionar_vertice(vertice) self.grafo.conectar(a, b) self.grafo.conectar(a, c) self.grafo.conectar(c, b) self.grafo.conectar(c, d) self.grafo.conectar(e, f) self.assertFalse(self.grafo.e_arvore()) def teste_obter_vertice_maior_grau(self): a = Vertice('a') b = Vertice('b') c = Vertice('c') d = Vertice('d') for vertice in {a,b,c,d}: self.grafo.adicionar_vertice(vertice) self.grafo.conectar(a, b) self.grafo.conectar(a, c) self.grafo.conectar(a, d) self.grafo.conectar(b, d) self.assertEqual(self.grafo.vertice_maior_grau().nome, a.nome) def teste_colocaracao(self): a = Vertice('a') b = Vertice('b') c = Vertice('c') d = Vertice('d') e = Vertice('e') f = Vertice('f') g = Vertice('g') h = Vertice('h') for vertice in {a, b, c, d, e, f, g, h}: self.grafo.adicionar_vertice(vertice) self.grafo.conectar(a, b) self.grafo.conectar(a, h) self.grafo.conectar(b, c) self.grafo.conectar(b, h) self.grafo.conectar(c, d) self.grafo.conectar(d, e) self.grafo.conectar(d, f) self.grafo.conectar(e, f) self.grafo.conectar(f, g) self.grafo.conectar(h, d) self.assertGreaterEqual(self.grafo.numero_cromatico(), 3)
def teste_grafo_vazio_nao_e_regular(self): grafo = Graph() self.assertFalse(grafo.e_regular())
def setUp(self): self.grafo = Graph()
def graphDF(database: str, table: str): metadata_db, index = get_metadata_db(database) if metadata_db: table_md: Table = metadata_db.get_table(table) if table_md: grafo = Graph() list_ui = list() list_pk = list() list_general: list = [x for x in range(table_md.get_nums_colums())] pk_list: list = table_md.get_pk_list() unique_list: list = table_md.unique.extractUnique() for pk in pk_list: pk_l = f"pk_{pk}" if pk_l not in list_pk: list_pk.append(pk_l) grafo.add_vertex(pk_l) if pk in list_general: list_general.remove(pk) for ui in unique_list: for ui_index in ui[2]: ui_l = f"ui_{ui_index}" if ui_l not in list_ui: list_ui.append(ui_l) grafo.add_vertex(ui_l) if ui_index in list_general: list_general.remove(ui_index) for general in list_general: grafo.add_vertex(general) if len(pk_list) == 0: hidden_pk = "Hidden_PK" grafo.add_vertex(hidden_pk) for ui in list_ui: grafo.join(hidden_pk, ui) for normal in list_general: grafo.join(ui, normal) grafo.join(hidden_pk, normal) else: for pk in list_pk: for ui in list_ui: grafo.join(pk, ui) for normal in list_general: grafo.join(pk, normal) for ui in list_ui: for normal in list_general: grafo.join(ui, normal) # print(f"{str(table_1)},{str(table_2)}") gra = grafo.graficar() ruta = f"graphDF_{database}" generate_grapviz(gra, str(ruta)) else: return "Tables empty" else: return 1
def graphThree(): grafo = Graph("n") grafo.addNodes(["A", "B", "C", "D", "E", "F", "G"]) grafo.addEdge("A", "B", 2) grafo.addEdge("A", "D", 3) grafo.addEdge("A", "C", 3) grafo.addEdge("B", "C", 4) grafo.addEdge("B", "E", 3) grafo.addEdge("C", "D", 5) grafo.addEdge("C", "F", 6) grafo.addEdge("C", "E", 1) grafo.addEdge("D", "F", 7) grafo.addEdge("E", "F", 8) grafo.addEdge("F", "G", 9) return grafo
def graphTwo(): grafo = Graph("d") grafo.addNode("S") grafo.addNode("A") grafo.addNode("B") grafo.addNode("C") grafo.addNode("D") grafo.addNode("E") grafo.addEdge("S", "A", 10) grafo.addEdge("S", "E", 8) grafo.addEdge("A", "C", 2) grafo.addEdge("E", "D", 1) grafo.addEdge("D", "A", -4) grafo.addEdge("D", "C", -1) grafo.addEdge("C", "B", -2) grafo.addEdge("B", "A", 1) return grafo
def graphOne(): grafo = Graph("d") grafo.addNode("A") grafo.addNode("B") grafo.addNode("C") grafo.addNode("D") grafo.addNode("E") grafo.addNode("F") grafo.addEdge("A", "B", 1) grafo.addEdge("A", "C", 8) grafo.addEdge("B", "D", 2) grafo.addEdge("C", "B", 3) grafo.addEdge("C", "E", 1) grafo.addEdge("D", "F", 1) grafo.addEdge("D", "C", 1) grafo.addEdge("D", "E", 3) grafo.addEdge("E", "D", 2) grafo.addEdge("E", "F", 9) # teste com valores negativos # grafo.addEdge("C", "A",-1) # grafo.addEdge("B", "C",-1) # Ciclo infinito bug na hora de rotanar a lista de Pai # grafo.addEdge("C", "E", -1) # grafo.addEdge("E", "D", -2) return grafo