travelling man in csharp

Here is an example of a program that implements a traveling salesman problem solution in C# using brute-force method which checks all possible permutations of cities to determine the shortest travel distance:

main.cs
using System;

class TravelingSalesman
{
    static int[,] distanceTable; // matrix of distances between cities
    static int numCities;

    static void Main()
    {
        Console.Write("Enter the number of cities: ");
        numCities = int.Parse(Console.ReadLine());

        distanceTable = new int[numCities, numCities];

        // read in distances between cities from user input
        for (int i = 0; i < numCities; i++)
        {
            for (int j = 0; j < numCities; j++)
            {
                if (i == j)
                {
                    distanceTable[i, j] = 0;
                }
                else
                {
                    Console.Write("Enter distance from city " + (i + 1) + " to city " + (j + 1) + ": ");
                    distanceTable[i, j] = int.Parse(Console.ReadLine());
                }
            }
        }

        int[] route = new int[numCities];
        // initialize route array to 0, 1, 2, ..., numCities-1
        for (int i = 0; i < numCities; i++)
        {
            route[i] = i;
        }

        int shortestDistance = int.MaxValue;
        int[] shortestRoute = new int[numCities];
        int count = 0; // counter for number of permutations checked

        // check all possible permutations of cities, starting from 0
        permuteCities(route, 0, ref shortestDistance, shortestRoute, ref count);

        Console.WriteLine("Shortest route: ");
        for (int i = 0; i < numCities; i++)
        {
            Console.Write((shortestRoute[i] + 1) + " ");
        }
        Console.WriteLine("\nShortest distance: " + shortestDistance);
        Console.WriteLine("Number of permutations checked: " + count);
        Console.ReadLine(); // pause console
    }

    static void permuteCities(int[] route, int currentIndex, ref int shortestDistance, int[] shortestRoute, ref int count)
    {
        // base case: end of array
        if (currentIndex == route.Length - 1)
        {
            int distance = calculateDistance(route);
            count++; // increment permutation count

            // update shortest distance and route if smaller distance found
            if (distance < shortestDistance)
            {
                shortestDistance = distance;
                Array.Copy(route, shortestRoute, route.Length);
            }
        }
        else
        {
            // recursively permute remaining array elements
            for (int i = currentIndex; i < route.Length; i++)
            {
                swap(ref route[currentIndex], ref route[i]);
                permuteCities(route, currentIndex + 1, ref shortestDistance, shortestRoute, ref count);
                swap(ref route[currentIndex], ref route[i]);
            }
        }
    }

    static int calculateDistance(int[] route)
    {
        int distance = 0;
        for (int i = 0; i < route.Length - 1; i++)
        {
            distance += distanceTable[route[i], route[i + 1]];
        }
        distance += distanceTable[route[route.Length - 1], route[0]]; // return to starting city
        return distance;
    }

    static void swap(ref int a, ref int b)
    {
        int temp = a;
        a = b;
        b = temp;
    }
}
3214 chars
101 lines

The program prompts the user to enter the number of cities and the distances between them, and then checks all permutations of cities to find the shortest travel distance. The permuteCities() method implements the recursive permutation algorithm, and the calculateDistance() method calculates the total travel distance for a given route. The swap() method is a helper method for swapping array elements. Finally, the program outputs the shortest route and distance found, as well as the number of permutations checked.

gistlibby LogSnag