Пример #1
0
        public void GetShortestRouteLengthTest3()
        {
            var distances = new float[,]
                {
                    { 0, 20, 20 },
                    { 20, 0, 20 },
                    { 20, 20, 0 }
                };

            var heldKarp = new HeldKarp(3, distances);

            var length = heldKarp.GetShortestRouteLength();

            length.Should().Be(60);
        }
Пример #2
0
        public void GetShortestRouteLengthTest()
        {
            var distances = new float[,]
                {
                    { 0, 2, 1, 4 },
                    { 2, 0, 3, 5 },
                    { 1, 3, 0, 6 },
                    { 4, 5, 6, 0 }
                };

            var heldKarp = new HeldKarp(4, distances);

            var length = heldKarp.GetShortestRouteLength();

            length.Should().Be(13);
        }
Пример #3
0
        public void GetShortestRouteLengthTest2()
        {
            var distances = new float[,]
                {
                    { 0, 20, 42, 35 },
                    { 20, 0, 30, 34 },
                    { 42, 30, 0, 12 },
                    { 35, 34, 12, 0 }
                };

            var heldKarp = new HeldKarp(4, distances);

            var length = heldKarp.GetShortestRouteLength();

            length.Should().Be(97);
        }
Пример #4
0
        public void Run()
        {
            using (var reader = new StreamReader("InputFiles//tsp.txt"))
            using (var writer = new StreamWriter("output.txt"))
            {
                string row = reader.ReadLine();
                int n = int.Parse(row);
                var coordinates = new float[n, 2];

                for (int i = 0; i < n; i++)
                {
                    row = reader.ReadLine();
                    var parts = row.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    var x = float.Parse(parts[0], CultureInfo.InvariantCulture);
                    var y = float.Parse(parts[1], CultureInfo.InvariantCulture);
                    coordinates[i, 0] = x;
                    coordinates[i, 1] = y;
                }

                var distances = new float[n, n];
                for (int i = 0; i < n; i++)
                    for (int j = i; j < n; j++)
                    {
                        if (i == j)
                        {
                            distances[i, j] = 0;
                        }
                        else
                        {
                            var distance = CalculateDistance(coordinates[i, 0], coordinates[i, 1], coordinates[j, 0], coordinates[j, 1]);
                            distances[i, j] = distance;
                            distances[j, i] = distance;
                        }
                    }

                var heldKarp = new HeldKarp(n, distances);

                var length = heldKarp.GetShortestRouteLength();

                writer.WriteLine(length);
            }
        }