def create_graph(G: Graph) -> None: print('\n[1] Read points from file') print('[2] Generate random points') if G.repr_type != RepresentationType.EMPTY: G.repr_type = RepresentationType.EMPTY print('\nOld graph has been deleted') points = [] option = input("\nChoose option\n") if option == '1': print("\nOk. Now put the file name.") filename = input('Filename: ') points = np.loadtxt(os.path.dirname(__file__) + '/' + filename, usecols=(0, 1)) G.load_data(get_graph_from_points(points), RepresentationType.ADJACENCY_MATRIX) print('\nGraph created!\n') if option == '2': print('\nPut number of points') num = int(input('Number: ')) for _ in range(num): points.append((2000 * random() - 1000, 2000 * random() - 1000)) G.load_data(get_graph_from_points(points), RepresentationType.ADJACENCY_MATRIX) print('\nGraph created!\n') return points
def generate_with_probability_menu(G: Graph) -> None: num_of_nodes = input("Put number of nodes:\n") probability = input("Put probability from range: (0;1):\n") data = get_graph_with_probability(int(num_of_nodes), float(probability)) G.load_data(data=data, representation_type=RepresentationType.ADJACENCY_MATRIX) display_submenu(G)
def generate_with_vertices_and_edges_menu(G: Graph) -> None: num_of_vertices = input("Put number of vertices:\n") num_of_edges = input("Put number of edges:\n") data = get_graph_by_vertices_and_edges(int(num_of_vertices), int(num_of_edges)) G.load_data(data=data, representation_type=RepresentationType.INCIDENCE_MATRIX) display_submenu(G)
def johnson_algorithm(graph: DirectedGraph) -> list: if graph.repr_type != RepresentationType.ADJACENCY_MATRIX: graph.to_adjacency_matrix() g = graph.repr # add vertex to graph and add edges of value 0 from this vertex to the rest g.append([0]) for i in range(len(g) - 2): g[len(g) - 1].append(0) for i in range(len(g)): g[i].append(None) graph.to_adjacency_list() edges = bellman_ford(graph, len(g)) edges.pop(len(g)) graph.to_adjacency_matrix() new_g = [[0 for x in range(len(g) - 1)] for y in range(len(g) - 1)] for i in range(len(new_g)): for j in range(len(new_g[i])): if g[i][j] is not None: new_g[i][j] = (g[i][j] + edges[i + 1] - edges[j + 1]) graph.repr.pop() for i in range(len(graph.repr)): graph.repr[i].pop() graph_for_dijkstra = Graph() data = get_graph_with_probability(len(graph.repr), 0.5) graph_for_dijkstra.load_data( data=data, representation_type=RepresentationType.ADJACENCY_MATRIX_WITH_WEIGHTS) for i in range(len(graph.repr)): for j in range(len(graph.repr[i])): graph_for_dijkstra.repr[i][j] = new_g[i][j] dist_matrix = [] for s in range(len(graph_for_dijkstra.repr)): print(f"For node [{s+1}]:") from_point = find_shortest_path( G=graph_for_dijkstra.get_weighted_adjacency_list(), start=s + 1, verbose=True) dist_matrix.append([]) for node in from_point: dist_matrix[s].append(from_point[node]) print() return dist_matrix
def transpose(G: Graph) -> Graph: """ Creates transpose graph of given directed graph. """ T = Graph() adjlist = [[] for _ in range(len(G.repr))] for i in range(len(G.repr)): for x in G.repr[i]: adjlist[x - 1].append(i + 1) T.load_data(adjlist, RepresentationType.ADJACENCY_LIST) return T
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_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 present_hamiltonian_graphs() -> None: """ Chceck if graph generated by 'get_graph_with_probability' with parameters given by user is Hamiltonian. """ 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) cycle = hamilton(G) if cycle == '[]': print("Graph is not hamiltonian") else: print("Graph is hamiltonian") print(cycle) GraphPlotter.plot_graph(G)
def generate_euler_graph_sequence(G: Graph, vertices: int) -> bool: """ Generates graphical sequence of Eulerian Graph for given number of vertices and loads it to graph given as a parameter. Returns True if generating was successful in one of num_samples iterations. """ num_samples = 100 iteration = 0 while iteration < num_samples: graphical_sequence = [(randint(2, vertices - 1) // 2) * 2 for _ in range(vertices)] if G.load_data(graphical_sequence, RepresentationType.GRAPH_SEQUENCE): return True iteration += 1 return False
def present_graphical_sequence() -> list: """ Presents graphical sequence inserting and checking [1]. Returns sequence as a list if sequence is graphical. Otherwise - [] - empty list. """ G = Graph() print('\nInsert sequence of integers') sequence = [int(x) for x in input('Your sequence: ').split()] if G.load_data(sequence, RepresentationType.GRAPH_SEQUENCE): print('Your sequence is graphical! Here is one of possible solutions\n') GraphPlotter.plot_graph(G) else: print('Sequence is not graphical\n') return [] return sequence
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_generators import gen_random_conn_graph_weighted if __name__ == "__main__": random_conn_graph = gen_random_conn_graph_weighted(10) G = Graph() G.load_data( data=random_conn_graph, representation_type=RepresentationType.ADJACENCY_MATRIX_WITH_WEIGHTS) print(G.get_distance_matrix())
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)
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 algorithms.hamilton import hamilton if __name__ == '__main__': G = Graph() G.load_data([[0, 1, 0, 1, 0],[1, 0, 1, 1, 1],[0, 1, 0, 0, 1],[1, 1, 0, 0, 1],[0, 1, 1, 1, 0]], \ RepresentationType.ADJACENCY_MATRIX) #adjacency matrix print(hamilton(G)) G.load_data([[0, 1, 0, 1, 0], [1, 0, 1, 1, 1],[0, 1, 0, 0, 1],[1, 1, 0, 0, 0], [0, 1, 1, 0, 0]], \ RepresentationType.ADJACENCY_MATRIX) #adjacency matrix print(hamilton(G))
import os, sys currentdir = os.path.dirname(os.path.realpath(__file__)) parentdir = os.path.dirname(currentdir) sys.path.append(parentdir) from utils.graph_plotter import GraphPlotter from utils.Graph import Graph, RepresentationType if __name__ == '__main__': G = Graph() G.load_data([4, 2, 2, 3, 2, 1, 4, 2, 2, 2, 2], RepresentationType.GRAPH_SEQUENCE) G.to_adjacency_matrix() GraphPlotter.plot_graph(G)
import os, sys, copy 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_generators import gen_random_conn_graph_weighted from utils.graph_plotter import GraphPlotter from algorithms.mst import kruskal, prim if __name__ == '__main__': v = int(input('Number of vertices: ')) G = Graph() G.load_data(gen_random_conn_graph_weighted(v), RepresentationType.ADJACENCY_MATRIX) GraphPlotter.plot_graph(G, draw_wages=True) MST = kruskal(G.repr) G.load_data(MST, RepresentationType.ADJACENCY_MATRIX) GraphPlotter.plot_graph(G, draw_wages=True) MST = prim(G.repr) G.load_data(MST, RepresentationType.ADJACENCY_MATRIX) GraphPlotter.plot_graph(G, draw_wages=True)
sys.path.append(parentdir) from random import random from utils.graph_plotter import GraphPlotter from utils.Graph import Graph, RepresentationType from utils.graph_generators import get_graph_from_points from algorithms.salesman import closest_neighbour, simulated_annealing if __name__ == '__main__': G = Graph() points = list() for _ in range(100): points.append((2000 * random() - 1000, 2000 * random() - 1000)) G.load_data(get_graph_from_points(points), RepresentationType.ADJACENCY_MATRIX) path = closest_neighbour(G) GraphPlotter.plot_points(points, path) path = simulated_annealing(G, 500, 100, 3) GraphPlotter.plot_points(points, path) points = np.loadtxt(os.path.dirname(__file__) + '/inputs/input59.dat', usecols=(0, 1)) G.load_data(get_graph_from_points(points), RepresentationType.ADJACENCY_MATRIX) path = closest_neighbour(G) GraphPlotter.plot_points(points, path) path = simulated_annealing(G, 500, 100, 10) GraphPlotter.plot_points(points, path)
import os, sys import numpy as np 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 utils.graph_generators import get_graph_with_probability, get_graph_by_vertices_and_edges if __name__ == "__main__": G = Graph() data = get_graph_with_probability(7, 0.5) G.load_data(data=data, representation_type=RepresentationType.ADJACENCY_MATRIX) print(G) GraphPlotter.plot_graph(G) G = Graph() data = get_graph_by_vertices_and_edges(5, 8) G.load_data(data=data, representation_type=RepresentationType.INCIDENCE_MATRIX) G.to_adjacency_matrix() print(G) GraphPlotter.plot_graph(G)