1

I'm trying to make a Java implementation for the Traveling Salesman Problem. I have read a lot about the different optimal algorithms and I found that the Held-Karp algorithm is a good one to implement. Now my problem is that I'm trying to make an adjacency matrix to store my edge values and then use these values to implement the algorithm and I cannot find a good logic to make the adjacency matrix.

My code reads from a file(graphFile) which has edges and their associated cost. The file is as follows:

A : B : 10
A : C : 3
A : D : 30
etc

Now my code reads the file, goes through each line and stores the vertices(nodes) in a SortedSet in the form of [A, B, C, D, ...]. Meanwhile, the edges are stored in a TreeMap along with their associated cost in the form of {AB = 10, AC = 3, ...}. I use my vertices set to define the size of my adjacency matrix and that is where I get stuck.

What would be the bet way on inserting the associated values of my edges in my adjacency matrix? Is that even a good way to solve the TSP or I'm on the wrong track?

static int[][] adjacencyMatrix;
static SortedSet<String> vertices  = new TreeSet<String>();
static TreeMap<String, Integer> edgeCost = new TreeMap<String, Integer>();

Here is a code snippet that should help. This piece of code reads the file and stores the values accordingly in the Map and Set:

while (graphFile.hasNextLine()) {
    line = graphFile.nextLine();
    tokenizer = line.split(" : "); 
    try {
        if (tokenizer.length != 3) {
            continue;
        }
        String source = tokenizer[0];
        String dest = tokenizer[1];
        String cost = tokenizer[2];
        addVertex(source, dest); //Add Vertex to set of vertices. Duplicates are removed.
        addEdge(source+dest, Integer.parseInt(cost)); //Add edges to an array then add array to list of edges.
    } catch (ArrayIndexOutOfBoundsException e) {
        System.err.println("Wrongly Formatted Line " + line);
    }
}

Now I have a code that computes my adjacencyMatrix. So far I managed to only make the diagonal part of the matrix(with value INF = 99999). However I have a logic problem of how to store the rest of the edgeCost values in the matrix.

private static void createAdjacencyMatrix() {

   for(int i=0; i<adjacencyMatrix.length; i++){
       for(int j=0; j<adjacencyMatrix.length; j++){
           if(i == j){
                adjacencyMatrix[i][j] = INF;
           }
       }
   }
   //Part where values of edgeCost should be added in matrix. Logic problem here.
}
}

Now I was thinking of, instead of making an adjacency matrix, I use an adjacency list. Which one would be better for the TSP using the Held-Karp algorithm?

nTuply
  • 1,364
  • 19
  • 42

1 Answers1

1

Here is a small example of what I suggested in the comments. It doesn't parse from a file, but I think you can handle it :-)

public static void main(String[] args){
    TreeMap<String, Map<String, Integer>> routes = new TreeMap<String, Map<String, Integer>>();

    // create some input
    String[] routeStrings ={"a : b : 10", "a : c : 3", "a : d : 30", "d : a : 7", "d : b : 5"};

    // parse and populate map
    for (String route: routeStrings){
        String[] sourceDestinationCost = route.split(" : ");
        String source = sourceDestinationCost[0];
        String destination = sourceDestinationCost[1];
        Integer cost = Integer.parseInt(sourceDestinationCost[2]);

        // create entry for source if needed
        if (!routes.containsKey(source)){
            routes.put(source, new TreeMap<String, Integer>());
        }
        routes.get(source).put(destination, cost);
    }

    // see result
    System.out.println(routes.toString());

}

This creates, for every source, a mapping to it's destinations, which are mapped to the cost.

In short the map can be viewed as a list of tuples of the form (source, destination, cost).

Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
  • I got it. So now could we say that it's an adjacency list? Or should I make an adjacency matrix based on these(and if I do, how do I do it). – nTuply May 16 '15 at 12:52