0

I wonder if there is a way to get the path (not only the distance) chosen by brute force algorithm (TSP)in my solution like i get only the distance.

Notice that I started from City Stockholm and ended at Stockholm.

  • The shortest path is city1 --> City2 --> City3 ----> .......then ---> City1 ?

A part of my main class:

    public void step(boolean[] wentTo, int currentCity, float distance)
    {
        int wentToCount = 0;
        for (int i = 1; i <= cityCount; ++i)
        {
            if (wentTo[i - 1])
            {
                ++wentToCount;
                continue;
            }
            boolean[] copy = new boolean[cityCount];
            System.arraycopy(wentTo, 0, copy, 0, cityCount);
            copy[i - 1] = true;
            float dist = distance + distances[distanceIndex(currentCity, i)];
            step(copy, i, dist);
        }
        if (wentToCount == cityCount)
        {
            if (shortest > distance)
            {
                shortest = distance;
            }
        }
    }
  • 1
    Isn’t it just a matter of registering in variables underway in your calculation? Resetting if your algorithm later decides on anoter route. – Ole V.V. Mar 26 '17 at 11:47
  • @OleV.V. Could please kindly show it in code ? –  Mar 26 '17 at 11:52

1 Answers1

0

The easy part: Just register in variables underway in your calculation. Resetting if your algorithm later decides on anoter route. The dificult part for an outsider not knowing your code is to try to fit it into the code that is at best hard to understand. Here’s an attempt nevertheless. For recording the route I am using two instance variables:

/** stack of numbers of towns on the route so far */ 
private Deque<Integer> route = new ArrayDeque<>();
/** shortest route found */
private List<Integer> solution;

To keep the first updated, do this before and after your recursive call:

        route.push(i);
        step(copy, i, dist);
        // after having tried town i, remove it from route again before trying next town
        int j = route.pop();
        assert j == i : "Got wrong town off stack, " + j + " instead of " + i;

This will make sure that once you have added all your towns to the route, they will also be in the route stack in the right order.

To keep the solution up-to-date, add a new value to it whenever you have found a shorter route:

        if (shortest > distance)
        {
            shortest = distance;
            solution = new ArrayList<>(route);
        }

Now when you print out the shortest distance, your solution holds the order of towns visited, so you can print it too.

Since I haven’t got your complete code, I have had no chance to test, so it may need to be polished here and there.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • I get error once i add private Deque route = new ArrayDeque<>(); I added my actual project in my main post underneath My project please take a look –  Mar 26 '17 at 12:45
  • You need to import `java.util.ArrayDeque` and `java.util.Deque`. What error message are you getting? – Ole V.V. Mar 26 '17 at 13:09
  • I did not understand this "do this before and after your recursive call" and the route is empty when i print it out ? –  Mar 26 '17 at 13:44
  • Did you try to print `route` or `solution`? `route` should be empty alright. `solution` shouldn’t. – Ole V.V. Mar 26 '17 at 13:46