Esempio n. 1
0
def do_testalg(testalg, G):
    if testalg[1]:
        print("\n\nTesting", testalg[0])
        startt = time()
        if testalg[0] == "Kruskal":
            ST = testalg[2](G)
            totalweight = 0
            for e in ST:
                totalweight += e.weight
        else:
            sorted_vertices = sorted(G.vertices, key=lambda v: v.label)
            testalg[2](G, sorted_vertices[0])
        endt = time()
        print("Elapsed time in seconds:", endt - startt)

        if testalg[0] != "Kruskal":
            print_max_dist(G)
            prepare_drawing(G)
        else:
            if len(ST) < len(G.vertices) - 1:
                print("Total weight of maximal spanning forest:", totalweight)
            else:
                print("Total weight of spanning tree:", totalweight)
            for e in G.edges:
                e.colornum = 0
            for e in ST:
                e.colornum = 1
            for v in G.vertices:
                v.label = v._label

        if WRITE_DOT_FILES:
            with open(os.path.join(os.getcwd(), testalg[3] + '.dot'),
                      'w') as f:
                write_dot(G, f, directed=testalg[4])
Esempio n. 2
0
def print_dot(filename, G):
    """
    Print a dot file with filename and graph
    :param filename: The file
    :param G: The graph
    """
    with open(filename, 'w') as f:
        write_dot(G, f)
Esempio n. 3
0
def color_graph(graph):
    # with open(name_file) as f:
    #     L = load_graph(f, read_list=True)
    #
    #
    # graph = L[0][0]
    # graph = graph.__add__(L[0][3])

    for v in graph.vertices:
        v.colornum = 0

    i = 0
    can_update = True
    old_color_length = 0
    while can_update:
        can_update = False
        # print("iteration:")
        # print(i)
        to_be_processed = graph.vertices
        colors = {}
        current_color = 0
        change_happened = False
        for active_vertex in graph.vertices:
            if active_vertex in to_be_processed:
                # print("active_vertex")
                # print(active_vertex)
                colors[current_color] = [active_vertex]
                to_be_processed.remove(active_vertex)
                for other_vertex in graph.vertices:
                    if other_vertex in to_be_processed:
                        if active_vertex.degree == other_vertex.degree:
                            if matching_neighbourhoods(active_vertex,
                                                       other_vertex):
                                colors[current_color].append(other_vertex)
                                to_be_processed.remove(other_vertex)
            current_color += 1
        for color in colors:
            for vertex in colors[color]:
                vertex.colornum = color

        # print("old color length:")
        # print(old_color_length)
        # print("current color length:")
        # print(len(colors))

        if old_color_length != len(colors):
            can_update = True

        old_color_length = len(colors)
        i += 1

    with open('colorful.dot', 'w') as f:
        write_dot(graph, f)
Esempio n. 4
0
def writedotgraph(graph):
    print("writing")
    with open('mygraph.dot', 'w') as j:
        graph_io.write_dot(graph, j)
Esempio n. 5
0
def print_dot(filename, G):
    with open(filename, 'w') as f:
        write_dot(G, f)