0

I have an exam coming up, and something we will be tested on is implementing the travelling salesman problem on undirected, weighted graphs. Here are examples of the type of problems we will be asked to solve:

Example 1: https://www.autonomousrobotslab.com/uploads/5/8/4/4/58449511/cs302_final_preparation_treasure_hunter.pdf

Example 2: https://www.autonomousrobotslab.com/uploads/5/8/4/4/58449511/cs302_final_preparation_tsp.pdf

However, the vast majority of videos/code I have looked at solve tsp with a complete graph, like this example: https://www.geeksforgeeks.org/traveling-salesman-problem-tsp-implementation/

Both of the examples my professor wants us to be able to solve are examples of incomplete graphs. Is there some modifications that could be done to the implementation at geeksforgeeks to account for an incomplete graph? I thought about simply putting in zeros in the adjacency matrix where the vertices aren't connected, but the code at geeksforgeeks assumes that all the vertices are connected to every other vertex, when in an incomplete graph, they aren't. I assume I would need some way to find all the permutations of vertices that are connected, versus finding all the permutations of the vertices in general.

  • Why not have a simple `if()` statement to determine if there is a connection, and if so, add the weight value? Also, do you have a compiler? If you do (and if you don't, there are plenty of free ones that you can download and install), it doesn't take a lot to simply take the code you found and simply experiment with it. As to what value to use to denote "no connection", that can be anything you can come up with that denotes this -- 0, -1, `std::numeric_limits::max()`, a `std::pair` where the bool is `true` or `false` telling you if there is a connection, etc. – PaulMcKenzie May 14 '19 at 03:32
  • 2
    Why not assign a very high cost to the missing edges? – Beta May 14 '19 at 03:39

1 Answers1

0

You can use next_permutation to get all the permutations for a vector. You iterate through all permutations, calculating the cost and keeping track of the lowest cost. Below is implementation for your second example.

int main(int argc, char** argv) {

    const int NUM_NODES = 5;

    int adjacencyMatrix[NUM_NODES][NUM_NODES] = {
        {0, 10, 16, 12, 8},
        {10, 0, 15, INT_MAX, 20},
        {16, 15, 0, 10, INT_MAX},
        {12, INT_MAX, 10, 0, 8},
        {8, 20, INT_MAX, 8, 0}
    };
    int min;
    int bestScore = INT_MAX;
    int currentScore = 0;
    vector<int> bestTrip;
    vector<int> trip;
    int legCost = 0;

    //set a default trip
    for (int i = 0; i < NUM_NODES; ++i) {
        trip.insert(trip.end(), i);
    }
    //insert trip back home
    trip.insert(trip.end(), 0);

    while (next_permutation(trip.begin() + 1, trip.end() - 1)) {
        currentScore = 0;
        for (int i = 0; i < NUM_NODES; i++) {
            legCost = adjacencyMatrix[trip[i]][trip[i + 1]];
            if (legCost == INT_MAX || currentScore == INT_MAX) {
                currentScore = INT_MAX;
            } else {
                currentScore += legCost;
            }
        }
        if (currentScore < bestScore) {
            bestScore = currentScore;
            bestTrip = trip;
        }
    }

    cout << "best trip: ";
    for (int i = 0; i < NUM_NODES + 1; i++) {
        cout << bestTrip[i];
    }
    cout << endl;
    cout << "best score:" << bestScore << endl;
    return 0;
}
Rik
  • 1,870
  • 3
  • 22
  • 35