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)
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)
def main(): k = randrange(0, 6) graph = generate_k_regular_graph(k) print(f"generated {k}_regular graph") plot_graph(graph)
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 if __name__ == "__main__": A = [4, 2, 2, 3, 2, 1, 4, 2, 2, 2, 2] B = [4, 4, 3, 1, 2] graph = generate_graph_from_graphic_string(A) plot_graph(graph) randomize_edges(graph, 5) plot_graph(graph)
def generate_connected_graph(max_vertex_count, min_weight, max_weight): graph = _generate_connected_graph(max_vertex_count) plot_graph(graph) add_connection_weights(graph, min_weight, max_weight) graph.print_graph_representation()