0

I want to implement a dynamic programming solution for travelling salesman problem. I have managed to solve it with the code below:

`package tsp;

import java.util.Arrays; import java.util.Collections;

public class TSP {

static int l ; public static void main(String[] args) {

    // sample input
    int[][] w = {{0,3,5,48,48,8,8,5,5,3,3,0,3,5,8,8,5},
                 {3,0,3,48,48,8,8,5,5,0,0,3,0,3,8,8,5},
                 {5,3,0,72,72,48,48,24,24,3,3,5,3,0,48,48,24},
                 {48,48,74,0,0,6,6,12,12,48,48,48,48,74,6,6,12},
            {48,48,74,0,0,6,6,12,12,48,48,48,48,74,6,6,12},
            {8,8,50,6,6,0,0,8,8,8,8,8,8,50,0,0,8},
            {8,8,50,6,6,0,0,8,8,8,8,8,8,50,0,0,8},
            {5,5,26,12,12,8,8,0,0,5,5,5,5,26,8,8,0},
            {5,5,26,12,12,8,8,0,0,5,5,5,5,26,8,8,0},
            {3,0,3,48,48,8,8,5,5,0,0,3,0,3,8,8,5},
            {3,0,3,48,48,8,8,5,5,0,0,3,0,3,8,8,5},
            {0,3,5,48,48,8,8,5,5,3,3,0,3,5,8,8,5},
            {3,0,3,48,48,8,8,5,5,0,0,3,0,3,8,8,5},
            {5,3,0,72,72,48,48,24,24,3,3,5,3,0,48,48,24},
            {8,8,50,6,6,0,0,8,8,8,8,8,8,50,0,0,8},
            {8,8,50,6,6,0,0,8,8,8,8,8,8,50,0,0,8},
            {5,5,26,12,12,8,8,0,0,5,5,5,5,26,8,8,0}};
    
    int n = w[0].length;
    int k = (int) Math.pow(2, (n - 1));
     int[][] p =new int[n][k];
     
     //ptint shortest cylce's  length
    System.out.println("The shortest cycle is of legnth "+ trvel(n, w, p));
    
    //print the shotrest cycle
    System.out.print("The shortest cycle is ");
    System.out.print("V0 ");
    print_path(0,p,k-1);
    System.out.print("V0 ");
}

private static int trvel(int n, int[][] w, int[][] p) {
    //the number of all subsets of vertices
    int k = (int) Math.pow(2, (n - 1));
    
    int[][] D = new int[n][k];
    
    //initializing D[A][v1]
    for (int i = 1; i < n; i++) {
        D[i][0] = w[i][0];
        
    }
    
    //finding the shortest path excluding v1
    for (int i = 1; i <= n - 2; i++) {
        for (int subset = 1; subset < k; subset++) {
            if (len(subset) == i) {
                for (int v = 1; v < n; v++) {
                    if (!haveI(subset, v-1)) {
                        D[v][subset] = min(v, w, D, subset, n);
                        
                        p[v][subset] = l;
                        
                    }

                }
            }
        }
    }
    int min = min(0, w, D, k-1, n);
    p[0][k-1] = l;
    return min;
}

// finding the cardinality of a subset
private static int len(int j) {
    int count = 0;
    while (j != 0) {
        j = j & (j - 1);
        count++;
    }
    return count;
}

//checking if Vi for some i belongs to a subset 
private static boolean haveI(int subset, int position) {
    int num = subset & ~(1 << (position));
    return (num & subset) != subset;
}

// finding the minimum of (W[v][j]+D[j][subsrt - v] for every j
private static int min(int v, int[][] w, int[][] D, int set, int n) {
    int[] m = new int[len(set)];
    int []i = new int[len(set)];
    int ind = 0;
        for(int j = 0 ; j< n-1 ; j++)
            if(haveI(set,j))
            {
                int num = set & ~(1 << (j));
                num = set & num;
                m[ind] = w[v][j+1]+D[j+1][num];
                i[ind]=j+1;
                ind++;
            }
        int min = m[0];
        l = i[0];
        for(int j = 1; j < len(set);j++)
            if(min>m[j]){
                min=m[j];
                l = i[j];
            }
                        
    return min;
   
}

//printing the shortest path
private static void print_path(int i, int[][] p, int n) {
   while(n > 0 ){
       System.out.print("V"+p[i][n]+" ");
       i =p[i][n];
       n = n& ~(1 << (i-1));
   }
}

}


`
Instead of writing the coordintes in the code like this:
`

public static void main(String[] args) {

    // sample input
    int[][] w = {{0,3,5,48,48,8,8,5,5,3,3,0,3,5,8,8,5},
                 {3,0,3,48,48,8,8,5,5,0,0,3,0,3,8,8,5},
                 {5,3,0,72,72,48,48,24,24,3,3,5,3,0,48,48,24},
                 {48,48,74,0,0,6,6,12,12,48,48,48,48,74,6,6,12},
            {48,48,74,0,0,6,6,12,12,48,48,48,48,74,6,6,12},
            {8,8,50,6,6,0,0,8,8,8,8,8,8,50,0,0,8},
            {8,8,50,6,6,0,0,8,8,8,8,8,8,50,0,0,8},
            {5,5,26,12,12,8,8,0,0,5,5,5,5,26,8,8,0},
            {5,5,26,12,12,8,8,0,0,5,5,5,5,26,8,8,0},
            {3,0,3,48,48,8,8,5,5,0,0,3,0,3,8,8,5},
            {3,0,3,48,48,8,8,5,5,0,0,3,0,3,8,8,5},
            {0,3,5,48,48,8,8,5,5,3,3,0,3,5,8,8,5},
            {3,0,3,48,48,8,8,5,5,0,0,3,0,3,8,8,5},
            {5,3,0,72,72,48,48,24,24,3,3,5,3,0,48,48,24},
            {8,8,50,6,6,0,0,8,8,8,8,8,8,50,0,0,8},
            {8,8,50,6,6,0,0,8,8,8,8,8,8,50,0,0,8},
            {5,5,26,12,12,8,8,0,0,5,5,5,5,26,8,8,0}};
    
    int n = w[0].length;
`
I want the coordintes to be read from a .atsp file named "testFile.atsp" instead of writing it in the code. The contents of the file should look like this:

`17

0,3,5,48,48,8,8,5,5,3,3,0,3,5,8,8,5
3,0,3,48,48,8,8,5,5,0,0,3,0,3,8,8,5
5,3,0,72,72,48,48,24,24,3,3,5,3,0,48,48,24
48,48,74,0,0,6,6,12,12,48,48,48,48,74,6,6,12
48,48,74,0,0,6,6,12,12,48,48,48,48,74,6,6,12
8,8,50,6,6,0,0,8,8,8,8,8,8,50,0,0,8
8,8,50,6,6,0,0,8,8,8,8,8,8,50,0,0,8
5,5,26,12,12,8,8,0,0,5,5,5,5,26,8,8,0
5,5,26,12,12,8,8,0,0,5,5,5,5,26,8,8,0
3,0,3,48,48,8,8,5,5,0,0,3,0,3,8,8,5
3,0,3,48,48,8,8,5,5,0,0,3,0,3,8,8,5
0,3,5,48,48,8,8,5,5,3,3,0,3,5,8,8,5
3,0,3,48,48,8,8,5,5,0,0,3,0,3,8,8,5
5,3,0,72,72,48,48,24,24,3,3,5,3,0,48,48,24
8,8,50,6,6,0,0,8,8,8,8,8,8,50,0,0,8
8,8,50,6,6,0,0,8,8,8,8,8,8,50,0,0,8
5,5,26,12,12,8,8,0,0,5,5,5,5,26,8,8,0
`


Can anyone show how this can be done.
Jeremy
  • 21
  • 4

0 Answers0