} // TSP_TreeBased public static int?buildCycle(Graph g, out Edge[] cycle) { cycle = null; int[] verticesOrder = new int[g.VerticesCount]; int orderCounter = 0; Graph tree; g.Kruskal(out tree); Predicate <int> preVisit = delegate(int i) { verticesOrder[orderCounter++] = i; return(true); }; tree.DFSearchFrom(0, preVisit, null); cycle = new Edge[g.VerticesCount]; int sum = 0; int weight; for (int i = 0; i < g.VerticesCount - 1; i++) { if (!g.GetEdgeWeight(verticesOrder[i], verticesOrder[i + 1]).HasValue) { cycle = null; return(null); } weight = g.GetEdgeWeight(verticesOrder[i], verticesOrder[i + 1]).Value; sum += weight; cycle[i] = new Edge(verticesOrder[i], verticesOrder[i + 1], weight); } weight = g.GetEdgeWeight(verticesOrder[tree.VerticesCount - 1], verticesOrder[0]).Value; sum += weight; cycle[tree.VerticesCount - 1] = new Edge(verticesOrder[tree.VerticesCount - 1], verticesOrder[0], weight); return(sum); }