Ejemplo n.º 1
0
def print_dijkstra(G: Graph, s):
    old_representation = G.representation_type
    graph.change_graph_representation_to(
        GraphRepresentationType.ADJACENCY_MATRIX)

    p_s, d_s = dijkstra(G, s)
    w = G.graph_weights

    G.print_graph_representation()
    G.print_weights()

    print("\nSTART: s =", s)

    for v in range(len(p_s)):
        path = []
        print('d({}) = {:>4} ===> '.format(v, d_s[v]), end='')

        currNode = v
        path.append(currNode)

        while p_s[currNode] != None:
            currNode = p_s[currNode]
            path.append(currNode)

        path = path[::-1]  # reversing
        print(path)

    plot_graph(graph)

    G.change_graph_representation_to(old_representation)
Ejemplo n.º 2
0
def check_if_eulerian(G: Graph):
    old_representation_type = G.representation_type
    G.change_graph_representation_to(GraphRepresentationType.ADJACENCY_LIST)

    old_graph_representation = copy.deepcopy(G.graph_representation)

    starting_vertex = 0

    path = []

    if(len(G.graph_representation) == 1):               # graf składa się z jednego wierzchołka
        return True, path
    else:
        for key, l in G.graph_representation.items():    # jezeli jakis wierzcholek jest odosobniony
            if(len(l) == 0):
                return False, path

    
    last_vertex = choose_edge_and_delete(G, starting_vertex, path)

    is_eulerian_graph = last_vertex == starting_vertex and check_if_graph_has_no_edges(G)

    G.graph_representation = old_graph_representation
    G.change_graph_representation_to(old_representation_type)

    return is_eulerian_graph, path
Ejemplo n.º 3
0
def find_connected_components(G: Graph):
    old_representation = G.representation_type
    graph.change_graph_representation_to(
        GraphRepresentationType.ADJACENCY_LIST)

    comp = components(graph)
    number_of_connected_components = max(comp)
    connected_components_list = []
    for i in range(number_of_connected_components):
        connected_components_list.append([])
        for j in range(len(G.graph_representation)):
            if (i + 1 == comp[j]):
                connected_components_list[i].append(j)

    for i in range(len(connected_components_list)):
        temp_str = f"{i + 1}: "
        for c in connected_components_list[i]:
            temp_str = temp_str + f"{c} "
        print(temp_str)

    plot_graph(graph, comp)

    G.change_graph_representation_to(old_representation)
Ejemplo n.º 4
0
def swap_two_pairs_of_nodes(graph: Graph):
    max_iter = 10000
    while True:
        a, b = get_random_two_connected_nodes(graph)
        c, d = get_random_two_connected_nodes(graph)
        matrix = graph.graph_representation

        if(a != d and b != c and a != c and b != d and matrix[a][d] == 0 and matrix[b][c] == 0):
            matrix[a][b] = matrix[b][a] = matrix[c][d] = matrix[d][c] = 0
            matrix[a][d] = matrix[d][a] = matrix[c][b] = matrix[b][c] = 1
            graph.change_graph_representation_to(GraphRepresentationType.ADJACENCY_LIST)
            checker = HamiltonChecker(graph)
            checker.check_hamilton(0)
            result = checker.is_hamiltionian()[0]
            graph.change_graph_representation_to(GraphRepresentationType.ADJACENCY_MATRIX)
            if result == False:
                matrix[a][b] = matrix[b][a] = matrix[c][d] = matrix[d][c] = 1
                matrix[a][d] = matrix[d][a] = matrix[c][b] = matrix[b][c] = 0
                continue
            return
        
        max_iter -= 1
        if max_iter == 0:
            raise Exception("Graph cannot be randomized")
Ejemplo n.º 5
0
from utils.graph import Graph,GraphRepresentationType
from utils.graphRandomizer import generate_Gnl_graph

if __name__ == "__main__":
    with open ('input/input.txt', 'r') as graph_input:
        graph_input = graph_input.readlines()
        representation_type = check_type_of_input(graph_input)
        parsed_graph_input = parse_graph_input(representation_type, graph_input)

        graph = Graph(representation_type, parsed_graph_input)
        graph.print_graph_representation()

        menu = '''
1. Change to ADJACENCY_LIST
2. Change to ADJACENCY_MATRIX
3. Change to INCIDENCE_MATRIX
4. EXIT
Your choice : '''
        while True:
            print(menu)
            choice = int(input())
            if 1 <= choice <= 3 :
                graph.change_graph_representation_to(GraphRepresentationType(choice))
            elif choice == 4:
                break
            else:
                print("Bad input, try again !")
                continue
            graph.print_graph_representation()

Ejemplo n.º 6
0
                    "1. ",
                    "2. 0 4",
                    "3. ",
                    "4. 0 2"]

    graph_input = graph_input2

    representation_type = check_type_of_input(graph_input)
    parsed_graph_input = parse_graph_input(representation_type, graph_input)

    graph = Graph(representation_type, parsed_graph_input)
    delete_edge(graph, 1, 3)

    graph = generate_Gnp_graph(5, 0.5)
    
    graph.change_graph_representation_to(GraphRepresentationType.ADJACENCY_LIST)
    
    graph.print_graph_representation()



    
    if_eulerian = check_if_eulerian(graph)
    print(f"Czy graf jest eulerowski: {if_eulerian}")

    if(if_eulerian):
        plot_graph(graph)

    findEulerianGraph = False

    if(findEulerianGraph):