for j in range(i+1,len(vertices)):
            distan=distancia(vertices[i],vertices[j])
            diccionario[(i,j)]=distan
            diccionario[(j,i)]=distan
            
    return diccionario,vertices

def distancia(punto1,punto2):
    return sqrt(((punto1[0]-punto2[0])*(punto1[0]-punto2[0]))+((punto1[1]-punto2[1])*(punto1[1]-punto2[1])))
 

    

#diccionario,vertices=read_file("pruebas-greedy/p00.vjt")
diccionario,vertices=read_file(sys.argv[1])
d = WeightingFunction(diccionario)
G = UndirectedGraph(E=d.keys())
MST = list(PrimsMinimumSpanningFinder().minimum_spanning_forest(G, d))
grafoPrim=UndirectedGraph(E=MST)

j=0
for inici in grafoPrim.V:
    distan=0
    solucio=list(DepthFirstTraverser().traverse(grafoPrim, inici))
    if (j==0):
        mejorDistancia=0
        for i in range(len(solucio)-1):
            mejorDistancia+=diccionario[(solucio[i],solucio[i+1])]
        mejorDistancia+=diccionario[(solucio[len(solucio)-1],solucio[0])]
        solucion=solucio
    
Esempio n. 2
0
#coding: latin1

#< full
from algoritmia.datastructures.digraphs import Digraph, WeightingFunction

d = WeightingFunction({
    (0, 1): 4,
    (0, 3): 4,
    (1, 4): 1,
    (2, 4): 0,
    (2, 5): 2,
    (3, 0): 1,
    (3, 1): 4,
    (4, 3): 1,
    (5, 5): 2
})
G = Digraph(E=d.keys())
for (u, v) in G.E:
    print('({}, {}): {}.'.format(u, v, d(u, v)), end=" ")
    #> full
 def setUp(self):
     self.iberia = UndirectedGraph(V=cities, E=roads)
     self.unconnected = Digraph(E={0: [1,2], 1:[2,3], 2:[5, 6], 3: [5], 4: [2], 5: [6]})
     self.unconnected_weight = WeightingFunction((((u,v), 1) for (u,v) in self.unconnected.E))
Esempio n. 4
0
#coding: latin1

#< full
from algoritmia.datastructures.digraphs import UndirectedGraph, WeightingFunction
from algoritmia.problems.spanningtrees import PrimsMinimumSpanningFinder

d = WeightingFunction({(0,1): 0, (0,2): 15, (0,3): 2, (1,3): 3, (1,4): 13, (2,3): 11,
                       (2,5): 4, (3,4): 5, (3,5): 8, (3,6): 12, (4,7): 9, (5,6): 16, 
                       (5,8):10, (6,7): 17, (6,8): 1, (6,9): 6, (7,9): 14, (8,9): 7}, 
                       symmetrical=True)
G = UndirectedGraph(E=d.keys())
MST = list(PrimsMinimumSpanningFinder().minimum_spanning_forest(G, d))
print('MST: {} con peso {}.'.format(MST, sum (d(u,v) for (u,v) in MST)))
#> full