def main(): graph = DirectedGraph() n = 5 graph.correct_random(n, 0.8) c = graph.kosaraju() while sum(c) / n != 1: graph.random(n, 1) c = graph.kosaraju() print(c) matrix = graph.get_adjacency_matrix() weights = graph.get_adjacency_matrix() for x in range(n): for y in range(n): if weights[x][y] != 0: weights[x][y] = random.randint(-5, 10) graph_utils.print_2d_array("Random digraph", matrix) graph_utils.print_2d_array("Weights on digraph", weights) graph_utils.print_2d_array("Johnson's Matrix", WeightedGraph.johnson(matrix, weights)) print("\nTest z zajęć \n") test_matrix = [[0, 1, 1], [1, 0, 0], [0, 1, 0]] test_weights = [[0, -1, -4], [4, 0, 0], [0, 2, 0]] graph_utils.print_2d_array( "Johnson's Matrix", WeightedGraph.johnson(test_matrix, test_weights))
def directed_graph_generate_from_undirected(undirected_graph, num_directed_edges, loops=True, has_odd_automorphism=None): num_vertices = len(undirected_graph) for h in nauty_generate_directed_from_undirected(undirected_graph, num_directed_edges, loops=loops): h = h.canonical_label() g = DirectedGraph(num_vertices, list(h.edges(labels=False))) g.canonicalize_edges() if has_odd_automorphism is None or directed_graph_has_odd_automorphism( g) == has_odd_automorphism: yield g
def main(): graph = DirectedGraph() graph.create_from_file_with_instance("simulated_annealing_input.txt", False) plt.rcParams["figure.figsize"] = (5, 10) cycle = generate_cycle(graph.graph_arr) fig, axs = plt.subplots(2, 1) axs[0].set_title("Initial data") draw_paths(cycle, axs[0]) cycle = simulated_annealing(cycle) axs[1].set_title("End data") draw_paths(cycle, axs[1]) plt.show()
def main(): graph = DirectedGraph() graph.random(5, 1) c = graph.kosaraju() while sum(c) / 5 != 1: graph.random(5, 1) c = graph.kosaraju() matrix = graph.get_adjacency_matrix() weights = graph.get_adjacency_matrix() for x in range(5): for y in range(5): if weights[x][y] != 0: weights[x][y] = random.randint(-5, 10) graph_utils.print_2d_array("Random digraph", matrix) graph_utils.print_2d_array("Weights on digraph", weights) b, d = bellman_ford(matrix, 0, weights) print(b) print(d)
def main(): graph = DirectedGraph() graph.create_from_file_with_instance() graph_utils.print_2d_array("Initial data", graph.graph_arr) graph_utils.print_2d_array("Adjacency matrix", graph.get_adjacency_matrix()) graph_utils.print_2d_array("Incidence matrix", graph.get_incidence_matrix()) graph_utils.print_2d_array("Adjacency list", graph.get_adjacency_list())
def main(): alfabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'] graph = DirectedGraph() graph.create_from_file_with_instance() graph_utils.print_2d_array("Adjacency matrix", graph.get_adjacency_matrix()) print("Błądzenie przypadkowe:") print("#########################") vec_rand = PageRank_random(graph, 100000, 0.15, 1) for i in range(graph_utils.get_vertices_number(graph)): print(alfabet[i], "==> PageRank = ", vec_rand[i]) print("#########################") print("\n\nWektor obsadzeń") print("#########################") vec_it = PageRank_iteration(graph, 54, 0.15) for i in range(graph_utils.get_vertices_number(graph)): print(alfabet[i], "==> PageRank = ", vec_it[i]) print("#########################") graph1 = DirectedGraph() graph1.create_from_file_with_instance('graph1.txt') graph_utils.print_2d_array("Adjacency matrix", graph1.get_adjacency_matrix()) print("Błądzenie przypadkowe:") print("#########################") vec_rand = PageRank_random(graph1, 1000000, 0.15, 1) for i in range(graph_utils.get_vertices_number(graph1)): print(alfabet[i], "==> PageRank = ", vec_rand[i]) print("#########################") print("\n\nWektor obsadzeń") print("#########################") vec_it = PageRank_iteration(graph1, 31, 0.15) for i in range(graph_utils.get_vertices_number(graph1)): print(alfabet[i], "==> PageRank = ", vec_it[i]) print("#########################")
def directed_graph_canonicalize(g): n = len(g) edges = g.edges() G, sigma = DiGraph([list(range(n)), edges]).canonical_label(certificate=True) new_edges = list(G.edges(labels=False)) edge_permutation = [ tuple([sigma[edge[0]], sigma[edge[1]]]) for edge in edges ] index_permutation = [new_edges.index(e) for e in edge_permutation] undo_canonicalize = [0] * n for k, v in sigma.items(): undo_canonicalize[v] = k return DirectedGraph( n, list(new_edges)), undo_canonicalize, selection_sort(index_permutation)
def main(): n = 7 test_adj_matrix = [[0 for x in range(n)] for y in range(n)] test_adj_matrix[0][1] = 1 test_adj_matrix[0][2] = 1 test_adj_matrix[0][4] = 1 test_adj_matrix[1][0] = 1 test_adj_matrix[1][2] = 1 test_adj_matrix[1][4] = 1 test_adj_matrix[1][3] = 1 test_adj_matrix[1][6] = 1 test_adj_matrix[2][5] = 1 test_adj_matrix[4][6] = 1 test_adj_matrix[3][6] = 1 test_adj_matrix[3][1] = 1 test_adj_matrix[5][1] = 1 test_adj_matrix[6][5] = 1 print("#############################################") graph_utils.print_2d_array("Example digraph 1", test_adj_matrix) c = DirectedGraph.kosaraju_with_adj_matrix(test_adj_matrix) #print(c) for i in range(len(c)): print("Wierzchołek ", i, " należy do składowej nr: ", c[i]) print("#############################################") print("\n\n") test_adj_matrix1 = [[0 for x in range(n)] for y in range(n)] test_adj_matrix1[0][1] = 1 test_adj_matrix1[0][6] = 1 test_adj_matrix1[6][0] = 1 test_adj_matrix1[6][1] = 1 test_adj_matrix1[1][2] = 1 test_adj_matrix1[2][1] = 1 test_adj_matrix1[2][3] = 1 test_adj_matrix1[2][4] = 1 test_adj_matrix1[4][3] = 1 test_adj_matrix1[4][5] = 1 test_adj_matrix1[5][1] = 1 test_adj_matrix1[5][2] = 1 print("#############################################") graph_utils.print_2d_array("Example digraph 2", test_adj_matrix1) c = DirectedGraph.kosaraju_with_adj_matrix(test_adj_matrix1) print(c) for i in range(len(c)): print("Wierzchołek ", i, " należy do składowej nr: ", c[i]) print("#############################################") print("\n\n#############################################") graph = DirectedGraph() graph_utils.print_2d_array("Random digraph", graph.correct_random(7, 0.3)) c = graph.kosaraju() print(c) for i in range(len(c)): print("Wierzchołek ", i, " należy do składowej nr: ", c[i]) print("#############################################")
def find_route(graph, v, w): """dfs of v vertex and use the result edge list to define what is the route between v and w: it begins from the last element of the route (w) to the first (v) surfing the list in the opposite direction""" route = "" edge_list = graph.dfs(v) if edge_list[w] is not None: if edge_list[w] == v: return str(v) + " ---> " + str(w) else: route += str(w) while edge_list[w] != v: route += " <--- " + str(edge_list[w]) w = edge_list[w] route += " <--- " + str(v) return route return "Route not present between " + str(v) + " and " + str(w) if __name__ == '__main__': graph = DirectedGraph(7) graph.add_edge(0, 6) graph.add_edge(0, 2) graph.add_edge(0, 1) graph.add_edge(0, 5) graph.add_edge(6, 4) graph.add_edge(4, 5) graph.add_edge(4, 3) graph.add_edge(5, 3) graph.dfs(2) print graph.edge_to print find_route(graph, 1, 2)
def directed_graph_from_encoding(digraph6_string): G = DiGraph(digraph6_string[1:]) return DirectedGraph(len(G.vertices()), list(G.edges(labels=False)))
def main(): graph = DirectedGraph() graph.create_from_file_with_instance() graph_utils.print_2d_array("Random digraph", graph.correct_random(5, 0.5))