public static (double, BidirectionalGraph <string, TaggedEdge <string, string> >) Get(UndirectedGraph <string, TaggedEdge <string, string> > graph) { var tsp = new tsp(); foreach (var v in graph.Vertices) { tsp.AddVertex(v); } foreach (var e in graph.Edges) { tsp.AddUndirectedEdge(e.Source, e.Target, double.Parse(e.Tag)); } var weightFunc = tsp.GetWeightsFunc(); var algorithm = new TSP <string, EquatableEdge <string>, BidirectionalGraph <string, EquatableEdge <string> > >(tsp.Graph, weightFunc); algorithm.Compute(); var path = algorithm.ResultPath; if (path != null) { var results = new results(); foreach (var v in path.Vertices) { results.AddVertex(v); } foreach (var e in path.Edges) { results.AddDirectedEdge(e.Source, e.Target, weightFunc(e)); } return(algorithm.BestCost, results.Graph); } else { return(algorithm.BestCost, null); } }
public static (double, BidirectionalGraph <string, EquatableEdge <string> >) Get(UndirectedGraph <int, Edge <int> > graph) { var tsp = new tsp(); foreach (var v in graph.Vertices) { tsp.AddVertex(v); } foreach (var e in graph.Edges) { tsp.AddUndirectedEdge(e.Source, e.Target, 1.0); } var algorithm = new TSP <string, EquatableEdge <string>, BidirectionalGraph <string, EquatableEdge <string> > >(tsp.Graph, tsp.GetWeightsFunc()); algorithm.Compute(); return(algorithm.BestCost, algorithm.ResultPath); }