3 import java
.util
.HashMap
;
8 protected final HashMap
<Integer
, CoordinatePair
<Integer
, Integer
>> Coordinates
;
10 Coordinate(Graph graph
) {
12 this.Coordinates
= new HashMap
<Integer
, CoordinatePair
<Integer
, Integer
>>(graph
.getNumOfCities());
15 Coordinate(Graph graph
, TSPLibReader tsplibreader
) {
17 this.Coordinates
= tsplibreader
.getCoordinates();
18 graph
.setNumOfCities(Coordinates
.size());
21 public int[][] computeDistances() {
23 int[][] Distance
= new int[graph
.getNumOfCities()][graph
.getNumOfCities()];
25 for (int k1
: Coordinates
.keySet()) {
26 for (int k2
: Coordinates
.keySet()) {
28 Distance
[k1
][k2
] = graph
.getINFINITY();
30 Distance
[k1
][k2
] = computeDistance(getCoordinates(k1
), getCoordinates(k2
));
37 public int computeDistance(CoordinatePair
<Integer
, Integer
> CityOne
,
38 CoordinatePair
<Integer
, Integer
> CityTwo
) {
39 /* pythagoras: a^2 + b^2 = c^2 => c = sqrt( a^2 + b^2 ) */
40 return (int)Math
.sqrt(
41 Math
.pow((double)(CityOne
.getFirst() - CityTwo
.getFirst()), 2) +
42 Math
.pow( (double)(CityOne
.getSecond() - CityTwo
.getSecond()), 2 ) );
46 double xd = (double)(CityOne.getFirst() - CityTwo.getFirst());
47 double yd = (double)(CityOne.getSecond() - CityTwo.getSecond());
48 double rij = Math.sqrt( (xd*xd + yd*yd) / 10.0 );
49 return (int)Math.round(rij);
53 public CoordinatePair
<Integer
, Integer
> getCoordinates(int City
) {
54 return Coordinates
.get(City
);