def hamilton(G: Graph, initial_vertex: int = 0) -> str: G.to_adjacency_matrix() path = [-1] * (len(G.repr) + 1) path[0] = path[-1] = initial_vertex return '[' + ' - '.join([str(x + 1) for x in path]) + ']' if hamilton_inner( G.repr, path, 1) else str([])
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_graph_randomization() -> None: """ Presents graph randomization for graph using graphical sequence and randomizations parameter. """ print('\nLet\'s start with Your own graphical sequence.\n') sequence = present_graphical_sequence() if sequence != []: randomizations = int(input('Number of randomizations: ')) G = Graph() G.load_data(sequence, RepresentationType.GRAPH_SEQUENCE) G.to_adjacency_matrix() randomize(G, randomizations) GraphPlotter.plot_graph(G)
def present_eulerian_graphs() -> None: """ Generates Eulerian Graph sequence and finds Eulerian Cycle. """ randomizations = 100 v = int(input('\nNumber of vertices: ')) G = Graph() if generate_euler_graph_sequence(G, v): print('Graphical sequence of graph: ' + str(G)) G.to_adjacency_matrix() randomize(G, randomizations) print('Euler Cycle: ' + euler_cycle(G)) GraphPlotter.plot_graph(G) else: print("Error while generating euler graph sequence")
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.")