def check_if_eulerian(G: Graph): old_representation_type = G.representation_type G.change_graph_representation_to("adjacency_list") old_graph_representation = copy.deepcopy(G.graph_representation) starting_vertex = 1 if (len(G.graph_representation[starting_vertex - 1]) == 0): for l in G.graph_representation: # jezeli jakis wierzcholek jest odosobniony if (len(l) == 0): return False if (len(G.graph_representation) == 1): return True else: return False last_vertex = choose_edge_and_delete(G, starting_vertex) is_eulerian_graph = last_vertex == starting_vertex and check_if_graph_has_no_edges( G) G.graph_representation = old_graph_representation G.change_graph_representation_to(old_representation_type) return is_eulerian_graph
def print_distance_matrix(G: Graph): G.print_graph_representation() m = calculate_distance_matrix(G) for i in range(len(m)): print(m[i])
def Dijkstra(G: Graph, s): p_s, d_s = _dijkstra(G, s) w = G.graph_representation G.print_graph_representation() print("\nSTART: s =", s) for v in range(len(p_s)): path = [] print('d({}) = {} ===> '.format(v + 1, d_s[v]), end='') currNode = v path.append(currNode + 1) while p_s[currNode] != None: currNode = p_s[currNode] path.append(currNode + 1) path = path[::-1] # reversing print(path)
def find_connected_components(G: Graph): old_representation = G.representation_type G.change_graph_representation_to("adjacency_list") comp = components(graph) number_of_connected_components = max(comp) connected_components_list = [] for i in range(number_of_connected_components): connected_components_list.append([]) for j in range(len(G.graph_representation)): if (i + 1 == comp[j]): connected_components_list[i].append(j + 1) for i in range(len(connected_components_list)): temp_str = f"{i + 1}: " for c in connected_components_list[i]: temp_str = temp_str + f"{c} " print(temp_str) plot_graph(graph, comp) G.change_graph_representation_to(old_representation)
def generate_graph_from_graphic_string(A): if not is_graphic_string(A): raise Exception("To nie jest ciag graficzny") list.sort(A, reverse=True) n = len(A) matrix = [[0 for i in range(n)] for j in range(n)] mod_A = [[i, A[i]] for i in range(n)] while sum([n[1] for n in mod_A]): list.sort(mod_A, key=lambda x: x[1], reverse=True) for i in range(1, mod_A[0][1] + 1): mod_A[i][1] = mod_A[i][1] - 1 mod_A[0][1] = mod_A[0][1] - 1 matrix[mod_A[i][0]][mod_A[0][0]] = 1 matrix[mod_A[0][0]][mod_A[i][0]] = 1 graph = Graph.create_graph_representation("adjacency_matrix", matrix) return graph
p_s, d_s = _dijkstra(G, i+1) distance_matrix.append(d_s) return distance_matrix def print_distance_matrix(G: Graph): G.print_graph_representation() m = calculate_distance_matrix(G) for i in range(len(m)): print(m[i]) if __name__ == "__main__": #examples graph = _generate_connected_graph(10) gr_cpy = deepcopy(graph.graph_representation) add_connection_weights(graph, 1, 10) print_distance_matrix(graph) graph_to_draw = Graph("adjacency_matrix", gr_cpy) plot_graph(graph_to_draw)
self.hamiltonianPath = self.stack.getListFromStack() self.stack.pop() def reset(self): self.hamiltonian_flag = False self.visited = [False]*len(graph.graph_representation) self.stack = Stack() self.hamiltonianPath = [] def is_hamiltionian(self): return {self.hamiltonian_flag : self.hamiltonianPath} if __name__ == "__main__": with open ('input/input6.txt', 'r') as graph_input: try: graph_input = graph_input.readlines() input_type = check_type_of_input(graph_input) graph = Graph.create_graph_representation(input_type, graph_input) checker = HamiltonChecker(graph) checker.check_hamilton(1) result = checker.is_hamiltionian() for is_hamiltonian,hamiltonian_path in result.items(): print(is_hamiltonian) print(hamiltonian_path) except BadInputException: print(BadInputException)