def __init__(self, G, w, graphFactory=lambda E: Digraph(E=E)): TspAsBranchAndBoundProblem2.__init__(self, G, w) if G.directed: Ginv = graphFactory(E=[(u,v) for (v,u) in w]) winv = WeightingFunction(((u,v), w(u,v)) for (v,u) in w) else: Ginv = G winv = w self.first_vertex = sample(G.V, 1)[0] self.D = dict(Dijkstra_shortest_distance_one_to_all(Ginv, winv, self.first_vertex))
#coding: latin1 #< full from algoritmia.problems.tsp import TspAsBranchAndBoundProblem from algoritmia.schemes.branchandbound import BranchAndBoundSolver from algoritmia.datastructures.prioritydicts import MinHeapMap from algoritmia.datastructures.graphs import UndirectedGraph, WeightingFunction w = 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=w.keys()) problem = TspAsBranchAndBoundProblem(G, w) solver = BranchAndBoundSolver(problem, lambda keyvalues: MinHeapMap(keyvalues)) x, weight = solver.solve() print('Camino', x, 'con peso', weight) #> full