def kosaraju(G: Graph) -> list: """ Finds strongly connected components in directed graphs using Kosaraju algorithm. """ G.to_adjacency_list() visited = [False for _ in range(len(G.repr))] stack = [] for i in range(len(G.repr)): if visited[i] == False: dfs(G.repr, i, visited, stack) T = transpose(G) stack.reverse() visited = [False for _ in range(len(G.repr))] groups = [0 for _ in range(len(G.repr))] group = [] index = 0 for i in stack: if visited[i] == False: dfs(T.repr, i, visited, group) for x in group: groups[x] = index # print_group(group, index) group = [] index += 1 return groups
def present_graph_from_file(G: Graph) -> None: operations_choice = '' v = len(G.repr) while operations_choice != 'b': display_submenu() operations_choice = input("Pick the option:\n") try: if operations_choice == '1': cycle = hamilton(G) if cycle == '[]': print('Graph is not Hamiltonian') else: print('Graph is Hamiltonian') print(cycle) if operations_choice == '2': cycle = euler_cycle(G) if cycle == '[]': print('Graph is not Eulerian') else: print('Graph is Eulerian') print(euler_cycle(G)) if operations_choice == '3': G.to_adjacency_matrix() for k in range(1, v): if G.is_k_regular(k): print('Graph is ' + str(k) + '-regular') break if k == v - 1: print('Graph is not k-regular') if operations_choice == '4': G.to_adjacency_matrix() randomizations = int(input('Number of randomizations: ')) randomize(G, randomizations) if operations_choice == '5': G.to_adjacency_list() groups = get_components(G) print_sorted_components(G, groups) GraphPlotter.plot_graph(G, False, False, groups) if operations_choice == '6': GraphPlotter.plot_graph(G) except: print("Something went wrong. Try again!")
def present_components_finding() -> None: """ Presents finding graph components for graph generated by 'get_graph_with_probability' with parameters given by user. """ vertices = int(input('\nNumber of vertices: ')) probability = float(input("Put probability (0;1):\n")) G = Graph() G.load_data(get_graph_with_probability(vertices, probability), RepresentationType.ADJACENCY_MATRIX) G.to_adjacency_list() groups = get_components(G) print_sorted_components(G, groups) GraphPlotter.plot_graph(G, False, False, groups)
def handle_convert(G: Graph) -> None: """ Graph representation type convert handler. """ convert_repr_type = '' print("\nPick representation type:") print("[1] Adjancency matrix") print("[2] Adjacency list") print("[3] Incidence matrix") convert_repr_type = input() if convert_repr_type == '1': G.to_adjacency_matrix() print_graph(G) elif convert_repr_type == '2': G.to_adjacency_list() print_graph(G) elif convert_repr_type == '3': G.to_incidence_matrix() print_graph(G) else: print("\nPut a valid option.")
import os, sys currentdir = os.path.dirname(os.path.realpath(__file__)) parentdir = os.path.dirname(currentdir) sys.path.append(parentdir) from utils.Graph import Graph, RepresentationType from utils.graph_plotter import GraphPlotter from algorithms.components import get_components, print_sorted_components if __name__ == '__main__': G = Graph() G.load_data([2, 2, 2, 1, 3, 1, 2, 1, 4, 2, 2, 1, 3, 1, 1], RepresentationType.GRAPH_SEQUENCE) G.to_adjacency_list() groups = get_components(G) print_sorted_components(G, groups) G.to_adjacency_matrix() GraphPlotter.plot_graph(G, False, False, groups)
def plot_graph(graph: Graph, draw_wages: bool = False, draw_arrows: bool = False, nodes_color_modes: list = None) -> None: """ Plots graph using Matplotlib. If color modes are not passed, the color of each node will be the same. """ is_digraph = isinstance(graph, DirectedGraph) current_repr_type = graph.repr_type if current_repr_type == RepresentationType.ADJACENCY_LIST \ or current_repr_type == RepresentationType.ADJACENCY_MATRIX \ or current_repr_type == RepresentationType.INCIDENCE_MATRIX \ or current_repr_type == RepresentationType.GRAPH_SEQUENCE \ or current_repr_type == RepresentationType.ADJACENCY_MATRIX_WITH_WEIGHTS: if current_repr_type != RepresentationType.ADJACENCY_MATRIX_WITH_WEIGHTS: graph.to_adjacency_matrix() source_matrix = graph.repr num_of_nodes = len(source_matrix) if nodes_color_modes is None: nodes_color_modes = [0] * num_of_nodes for i in range(len(nodes_color_modes)): if nodes_color_modes[i] > len(GraphPlotter.node_color_modes) - 1: nodes_color_modes[i] = 0 ax = GraphPlotter.__prepare_plot() node_positions = GraphPlotter.__get_node_positions(num_of_nodes) GraphPlotter.__draw_edges(num_of_nodes=num_of_nodes, source_matrix=source_matrix, node_positions=node_positions, digraph=is_digraph) if draw_wages: GraphPlotter.__draw_wages(num_of_nodes=num_of_nodes, source_matrix=source_matrix, node_positions=node_positions, label_offset=0.25, digraph=is_digraph) if draw_arrows: GraphPlotter.__draw_arrows(num_of_nodes=num_of_nodes, source_matrix=source_matrix, node_positions=node_positions, arrow_size=0.05, digraph=is_digraph) GraphPlotter.__draw_nodes(num_of_nodes=num_of_nodes, ax=ax, groups=nodes_color_modes, node_positions=node_positions) plot.show() if current_repr_type == RepresentationType.INCIDENCE_MATRIX: graph.to_incidence_matrix() elif current_repr_type == RepresentationType.ADJACENCY_LIST: graph.to_adjacency_list() else: print("Incorrect graph type.")