예제 #1
0
def random_weighted_graph_resolver(data, _):
    def random_matrix(size, p):
        adj_matrix = [['.'] * size for i in range(size)]

        for i in range(size):
            for j in range(i, size):
                if i != j and random.random() < p:
                    # 0.4 - probability that edge between i and j exists
                    adj_matrix[i][j] = adj_matrix[j][i] = 1
        return adj_matrix

    size = int(data.split(' ')[0])
    p = float(data.split(' ')[1])
    if size < 0:
        return "Size cannot be a negative number"
    matrix = random_matrix(size, p)
    graph = Graph.from_cost_matrix(matrix)
    components = Algorithms.max_connected_comp(graph)
    while len(components) > 1:
        matrix = random_matrix(size, p)
        graph = Graph.from_cost_matrix(matrix)
        components = Algorithms.max_connected_comp(graph)

    for i in range(size):
        for j in range(i, size):
            if matrix[i][j] == 1:
                matrix[i][j] = matrix[j][i] = random.randrange(1, 11)
    s = ""
    for row in matrix:
        s += f"{'  '.join([f' {cell}' if len(str(cell)) == 1 else f'{cell}' for cell in row])}\n"

    return s
예제 #2
0
def hamilton_cycle_resolver(graph: Graph, args):
    result = Algorithms.hamiltonian_cycle(graph)

    if result is None:
        return 'Given graph does not have an hamiltonian cycle'
    else:
        return result
예제 #3
0
def kruskal_resolver(graph, _):
    minimal_spanning_tree = Algorithms.kruskal(graph)

    result_str = ""
    for row in minimal_spanning_tree:
        result_str += f"{'  '.join([f' {cell}' if len(str(cell)) == 1 else f'{cell}' for cell in row])}\n"

    return result_str
예제 #4
0
def distance_matrix_resolver(graph, _):
    result = Algorithms.distance_matrix(graph)
    s = ""

    for row in result:
        s += f"{'  '.join([f' {cell}' if len(str(cell)) == 1 else f'{cell}' for cell in row])}\n"

    return s
예제 #5
0
def create_random_eulerian_resolver(data, _):
    nodes = int(data)

    eulerian_graph = Algorithms.create_random_eulerian(nodes)
    adjacency_list = AdjacencyList(eulerian_graph)
    print(Graph.from_adjacency_list(adjacency_list))
    euler_cycle = AdjacencyList.find_euler_path(adjacency_list, Node(0))
    return euler_cycle
예제 #6
0
def graphical_sequence():
    data = input('Enter number sequence:\n')
    sequence = [int(number) for number in data.split(' ')]

    graph = Algorithms.degree_seq_to_graph(sequence)

    if graph is None:
        raise Error("Given sequence does not represent a valid graph")

    return graph
예제 #7
0
def k_regular_graph_resolver(args, _):

    [nodes, degree] = [int(num) for num in args.split(' ')]

    result = Algorithms.generate_random_regular_graph(degree, nodes)
    graph = Graph()
    adj_matrix = AdjacencyMatrix(graph)
    adj_matrix.matrix = result

    return adj_matrix
예제 #8
0
def johnson_resolver(graph, _):
    try:
        result = Algorithms.johnson(graph)

        result_str = ""
        for row in result:
            result_str += f"{'  '.join([f' {cell}' if len(str(cell)) == 1 else f'{cell}' for cell in row])}\n"

        return result_str
    except Exception as e:
        return str(e)
예제 #9
0
def max_connected_comp_resolver(graph: Graph, args):
    components = Algorithms.max_connected_comp(graph)

    result = ""
    max_component_index = 0
    for i, component in enumerate(components):
        result += f"{i}) {component}\n"
        if len(component) == max([len(comp) for comp in components]):
            max_component_index = i

    result += f"Largest component have an index of {max_component_index}\n"
    return result
예제 #10
0
def dijkstra_resolver(graph, _):
    d_s, p_s = Algorithms.dijkstra(graph, 0)

    result = ""
    for node, distance in enumerate(d_s):
        prev = p_s[node]
        path = [str(node)]

        while prev is not None:
            path.append(str(prev))
            prev = p_s[prev]

        path.reverse()
        result += f"d({node}) = {distance}, path: [{' - '.join(path)}]\n"

    return result
예제 #11
0
def center_resolver(graph, _):
    center, min_row_sum = Algorithms.graph_center(graph)
    center_minimax, minimax_distance = Algorithms.graph_minimax_center(graph)

    return f"center node id: {center} with sum of {min_row_sum}\nminimax center node id: {center_minimax} with minimax distance of {minimax_distance}\n"