Exemple #1
0
 def _testBenchmark(self):
     print("file", "vertices", "edges", "time", sep=', ')
     for filename in [
             "tinyDG.txt", "mediumDG.txt", "largeDG.txt", "XtraLargeDG.txt"
     ]:
         graph = parseGraph(filename, AdjacencyList(), True)
         before_time = default_timer()
         mst = prim(graph, 0)
         time = default_timer() - before_time
         print(filename, graph.V, graph.E, time, sep=', ')
Exemple #2
0
def group(alg, k, data):
    graph = Graph()

    for coordinates in data:
        graph.add_connected_vertice(*coordinates)

    if alg == 'prim':
        mst = prim(graph, k)
    else:
        mst = kruskal(graph, k)

    return mst
Exemple #3
0
def group(alg, k, data):
    graph = Graph()

    for coordinates in data:
        graph.add_connected_vertice(*coordinates)

    if alg == 'prim':
        mst = prim(graph, k)
    else:
        mst = kruskal(graph, k)

    return mst
Exemple #4
0
def main():
    n = int(input('Podaj liczbę wierzchołków: '))
    G = Graph(n)
    G.rand_graph()
    G.choose_biggest_comp()
    G.assign_weights()
    print(f"Wylosowano graf o {G.n} wierzchołkach:")
    print(G)

    print(f"Podaj wierzchołek startowy od 0 do {G.n - 1}: ")
    graph = nx.from_numpy_matrix(G.adj)
    graph.edges(data=True)
    plt.subplot(111)
    pos=nx.circular_layout(graph)
    nx.draw_networkx(graph, pos)
    labels = nx.get_edge_attributes(graph, 'weight')
    nx.draw_networkx_edge_labels(graph, pos, edge_labels=labels)
    plt.show()

    #start = int(input(f'Podaj wierzchołek startowy od 0 do {G.n - 1}: '))
    start = int(input())
    D = Dijkstra()
    D.do_dijkstra(G.adj, G.adj, start)

    print(f"START: s = {start}")
    for v in range(G.n):
        print(f'd({v}) = {D.ds[v]} ==> [{get_ps(v, D)}]')

    print("\nMacierz odległości:")
    matrix = make_distance_matrix(G)
    print(matrix)
    centr = get_center(matrix)

    print(f"\nCentrum grafu: {centr}")
    print(f"Suma w centrum grafu: {sum(matrix[centr])}")
    minimax = get_minimax_center(matrix)

    print(f"\nCentrum minimax: {minimax}")
    print(f"Odległość do najdalszego wierzchołka: {max(matrix[minimax])}")

    print("\nMinimalne drzewo rozpinające:")
    [mst, sum_mst] = prim(G)
    print(mst)
    print(f"Suma wag krawędzi: {sum_mst}")

    plt.subplot(111)
    graph = nx.from_numpy_matrix(G.adj)
    mst = nx.from_numpy_matrix(mst)

    pos = nx.circular_layout(graph)

    colors = []
    for u,v in graph.edges():
        if [u,v] not in mst.edges:
            colors.append('b')
        else:
            colors.append('r')

    graph.edges(data=True)
    nx.draw(graph, pos, with_labels=True, edge_color=colors)
    labels = nx.get_edge_attributes(graph, 'weight')
    nx.draw_networkx_edge_labels(graph, pos, edge_labels=labels)
    plt.show()
Exemple #5
0
from tour import Tour
import random

import mst

xy = reader.read_xy("input/berlin52.tsp")
t = TwoOpt(xy)
t.optimize()

best = t.tour.node_ids[:]
best_length = t.tour.tour_length()
print("local optimum: " + str(best_length))

points = 0

mst_edges = mst.prim(t.tour.xy)

mst_connectivity = [[]] * len(t.tour.xy)
for e in mst_edges:
    mst_connectivity[e[0]].append(e[1])
    mst_connectivity[e[1]].append(e[0])

simultaneous_edges = 10

for i in range(50):
    print("iteration: " + str(i))
    new_edges = mst.get_new_mst_edges(t.tour, mst_edges)
    improved = False
    ee = random.sample(new_edges, simultaneous_edges)
    for e in ee:
        m = basic.midpoint(xy, e[0], e[1])
Exemple #6
0
 def test_prim_groups(self):
     three_groups = prim(self.g, 3)
     self.assertGroupsEqual(three_groups, self.three_groups)
Exemple #7
0
 def test_prim(self):
     mst = prim(self.g)
     self.assertGroupsEqual(mst, self.mst)
Exemple #8
0
    def testTinyDG(self):
        graph = parseGraph("tinyDG.txt", AdjacencyList(), True)
        mst = prim(graph, 0)

        self.assertEqual(mst, ([None, 5, 0, 1, 5, 7, 3, 2
                                ], [0, .32, .26, .29, .35, .28, .52, .34]))