patternjavaMinor
Calculating Travelling Salesman - memory consumption and speed
Viewed 0 times
salesmantravellingcalculatingmemoryandspeedconsumption
Problem
I need to implement the dynamic programming algorithm for the travelling salesman problem. My input is a text file with first line indicates the number of cities. Each city is a point in the plane, and each subsequent line indicates the x- and y-coordinates of a single city. The distance between two cities is defined as the Euclidean distance. The output of my program should be the minimum cost of a travelling salesman tour for this instance, rounded down to the nearest integer. I am having trouble coming up with ideas on how to reduce memory consumption and increase efficiency. The optimizations I have used are only storing subproblems of size m and size m-1 in an array.
Could someone advise me on possible improvements?
```
public class TSP {
static double [][] A;
static List cities;
static int number_of_cities;
static List> myPowerSetPrevious;
static List> myPowerSetNext;
static int sizeTogether;
public static double distance (double x1, double y1, double x2, double y2){
return Math.sqrt(Math.pow(x1-x2,2) + Math.pow(y1-y2,2));
}
private static void getSubsets(List superSet, int k, int idx, Set current,List> solution) {
//successful stop clause
if (current.size() == k) {
Set a = new HashSet();
a.addAll(current);
solution.add(a);
return;
}
//unseccessful stop clause
if (idx == superSet.size()) return;
Integer x = superSet.get(idx);
current.add(x);
//"guess" x is in the subset
getSubsets(superSet, k, idx+1, current, solution);
if (x != 1){
current.remove(x);
//"guess" x is not in the subset
getSubsets(superSet, k, idx+1, current, solution);
}
}
public static List> getSubsets(List superSet, int k) {
List> res = new ArrayList>();
getSubsets(superSet, k, 0, new HashSet(), res);
return res;
}
public static void initialize(int k_start){
int presize = myPowerSetPrevious.size(); // size of previous (1)
int oldSize = sizeTogether;
myPowerSe
Could someone advise me on possible improvements?
```
public class TSP {
static double [][] A;
static List cities;
static int number_of_cities;
static List> myPowerSetPrevious;
static List> myPowerSetNext;
static int sizeTogether;
public static double distance (double x1, double y1, double x2, double y2){
return Math.sqrt(Math.pow(x1-x2,2) + Math.pow(y1-y2,2));
}
private static void getSubsets(List superSet, int k, int idx, Set current,List> solution) {
//successful stop clause
if (current.size() == k) {
Set a = new HashSet();
a.addAll(current);
solution.add(a);
return;
}
//unseccessful stop clause
if (idx == superSet.size()) return;
Integer x = superSet.get(idx);
current.add(x);
//"guess" x is in the subset
getSubsets(superSet, k, idx+1, current, solution);
if (x != 1){
current.remove(x);
//"guess" x is not in the subset
getSubsets(superSet, k, idx+1, current, solution);
}
}
public static List> getSubsets(List superSet, int k) {
List> res = new ArrayList>();
getSubsets(superSet, k, 0, new HashSet(), res);
return res;
}
public static void initialize(int k_start){
int presize = myPowerSetPrevious.size(); // size of previous (1)
int oldSize = sizeTogether;
myPowerSe
Solution
The optimization I have used so far is storing subproblems of size m and size m-1 in HashMaps separately including only subsets with 1st vertex. E.g. For m = 1, there is only {1} subset. The keys of this HashMap are BitSets as used to represent subsets. I also used Gosper's Hack to get the next subset with same number of m bits, putting new BitSet key and double[] value in the HashMap for m, this way increasing efficiency by generating subsets iteratively.
As well, I created an adjacency matrix of distances (n x n) based on the number of cities to use for lookup, which also increases efficiency.
As well, I created an adjacency matrix of distances (n x n) based on the number of cities to use for lookup, which also increases efficiency.
Context
StackExchange Code Review Q#32574, answer score: 4
Revisions (0)
No revisions yet.