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 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
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 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)