1 import java
.util
.HashMap
;
6 protected final HashMap
<Integer
, CoordinatePair
<Integer
, Integer
>> Coordinates
;
8 Coordinate(Graph graph
) {
10 this.Coordinates
= new HashMap
<Integer
, CoordinatePair
<Integer
, Integer
>>(graph
.getNumOfCities());
13 Coordinate(Graph graph
, TSPLibReader tsplibreader
) {
15 this.Coordinates
= tsplibreader
.getCoordinates();
16 graph
.setNumOfCities(Coordinates
.size());
19 public int[][] computeDistances() {
21 int[][] Distance
= new int[graph
.getNumOfCities()][graph
.getNumOfCities()];
23 for (int k1
: Coordinates
.keySet()) {
24 for (int k2
: Coordinates
.keySet()) {
26 Distance
[k1
][k2
] = graph
.getINFINITY();
28 Distance
[k1
][k2
] = computeDistance(getCoordinates(k1
), getCoordinates(k2
));
35 public int computeDistance(CoordinatePair
<Integer
, Integer
> CityOne
,
36 CoordinatePair
<Integer
, Integer
> CityTwo
) {
37 /* pythagoras: a^2 + b^2 = c^2 => c = sqrt( a^2 + b^2 ) */
38 return (int)Math
.sqrt(
39 Math
.pow((double)(CityOne
.getFirst() - CityTwo
.getFirst()), 2) +
40 Math
.pow( (double)(CityOne
.getSecond() - CityTwo
.getSecond()), 2 ) );
44 double xd = (double)(CityOne.getFirst() - CityTwo.getFirst());
45 double yd = (double)(CityOne.getSecond() - CityTwo.getSecond());
46 double rij = Math.sqrt( (xd*xd + yd*yd) / 10.0 );
47 return (int)Math.round(rij);
51 public CoordinatePair
<Integer
, Integer
> getCoordinates(int City
) {
52 return Coordinates
.get(City
);