// TSP.java class Graph { // the matrix stores the edge information private int[][] matrix; // this stores the values being stored by this graph private Type[] values; // the size of the graph private int size; // set the graph as empty public Graph(int size) { matrix = new int[size][size]; this.size = size; for(int i = 0; i < size; i++) { for(int j = 0; j < size; j++) { matrix[i][j] = 1000000; } } // make space for the values (and ignore the cast warning) @SuppressWarnings("unchecked") Type[] values = (Type[]) new Object[size]; this.values = values; } // lookup a node number by value public int lookup(Type value) { for (int i = 0; i < size; i++) { if (values[i].equals(value)) { return i; } } return -1; } // insert an edge by index public void insertEdge(int from, int to, int cost) { matrix[from][to] = cost; matrix[to][from] = cost; } // insert an edge by value public void insertEdge(Type from, Type to, int cost) { int fromIndex = lookup(from); int toIndex = lookup(to); insertEdge(fromIndex, toIndex, cost); } // remove an edge public void removeEdge(int from, int to) { matrix[from][to] = 0; } // return the cost of an edge or 0 for none public int getCost(int from, int to) { return matrix[from][to]; } // add a node's data to the graph public void setValue(int node, Type value) { values[node] = value; } // return the size of the graph public int getSize() { return size; } // get the value of a node public Type getValue(int index) { return values[index]; } } public class TSP { // varaibles for best path so far private static int best_cost = Integer.MAX_VALUE; private static int[] best_path; // run through all permutations public static void permute(Graph graph, int[] array, int start, int end) { int N = graph.getSize(); // if this is the end, consider this path if (start == end) { int total_cost = 0; // add up this route for (int i = 0; i < (N - 1); i++) { total_cost = total_cost + graph.getCost(array[i], array[i + 1]); } // add end city back to start total_cost = total_cost + graph.getCost(array[N - 1], array[0]); // check if this is the best so far if (total_cost < best_cost) { best_cost = total_cost; for (int i = 0; i < N; i++) { best_path[i] = array[i]; } } } else { // for each node in the array for (int i = start; i <= end; i++) { // swap this with the start int temp = array[start]; array[start] = array[i]; array[i] = temp; // permute all after the start permute(graph, array, start + 1, end); // swap it back temp = array[start]; array[start] = array[i]; array[i] = temp; } } } public static void shortestTour(Graph graph) { int N = graph.getSize(); // make an array of the nodes int[] nodes = new int[graph.getSize()]; for (int i = 0; i < N; i++) { nodes[i] = i; } // run through all permutations // we start at 1 since first location can be arbitrarily fixed permute(graph, nodes, 1, N - 1); // print the results System.out.println("The tour should go through: "); for (int i = 0; i < N; i++) { System.out.println(graph.getValue(best_path[i]) + ", "); } System.out.println("and back to " + graph.getValue(best_path[0]) + "."); System.out.println("For a total cost of " + best_cost); } public static void main(String args[]) { // create a graph with 6 connected nodes Graph graph = new Graph(6); graph.setValue(0, "A"); graph.setValue(1, "B"); graph.setValue(2, "C"); graph.setValue(3, "D"); graph.setValue(4, "E"); graph.setValue(5, "F"); graph.insertEdge("A", "B", 13); graph.insertEdge("A", "C", 29); graph.insertEdge("A", "D", 31); graph.insertEdge("A", "F", 48); graph.insertEdge("B", "C", 13); graph.insertEdge("B", "D", 23); graph.insertEdge("B", "E", 26); graph.insertEdge("B", "F", 15); graph.insertEdge("C", "E", 24); graph.insertEdge("C", "F", 18); graph.insertEdge("D", "E", 14); best_path = new int[6]; // compute the shortest tour shortestTour(graph); } }