Example #1
0
        static void Main(string[] args)
        {
            // Example

            var g =
                @"7
- 0 2 4 1 5 1
5 - 0 0 4 2 2
5 4 - 1 0 4 1
0 2 3 - 2 0 6
2 2 1 2 - 4 0
5 2 4 0 3 - 6
2 3 5 0 0 4 -
";

            int?[,] weights = GraphUtil.FromMatrixFormat(g);
            var cycles = new int[][]
            {
                new LatinComposition(weights).GetShortestHamiltonianCycle(),
                new BranchAndBound(weights).GetShortestHamiltonianCycle(),
            };

            foreach (var cycle in cycles)
            {
                // 4
                int distance = GraphUtil.RouteDistance(weights, cycle);
                // 1 2 3 5 7 6 4 1
                string cycleString = string.Join(" ", cycle.Select(vertex => (char)(vertex + '1')));

                Console.WriteLine("The distance = " + distance);
                Console.WriteLine("The path:\n" + cycleString);
                Console.WriteLine("\n");
            }
        }
Example #2
0
        public static AdjacencyMatrix GetGraph(int inputNumber)
        {
            // get weights
            var lines   = ReadLinesFromBuiltInInput($"{CurrentNamespace}.inputs.in{inputNumber}.txt");
            var weights = GraphUtil.FromMatrixFormat(lines);

            // get paths
            lines = ReadLinesFromBuiltInInput($"{CurrentNamespace}.inputs.in{inputNumber}_res.txt");
            var(n1, shortestPathIndex) = lines[0].Split(' ').Deconstruct(int.Parse);
            var paths = new List <int[]>(n1);

            for (int i = 0; i < n1; i++)
            {
                var path = lines[i + 1].Split(' ').Select(int.Parse).ToArray();
                paths.Add(path);
            }

            return(new AdjacencyMatrix
            {
                Weights = weights,
                AllPaths = paths,
                ShortestPathIndex = shortestPathIndex,
            });
        }