'How do I apply the Graphhopper library for coordinates with given latitude/longitude?

I want to calculate the shortest route from one to many places with coordinates (latitude, longitude).

The question is now how to apply the Graphhopper library for coordinates with given latitude/longitude.

For example with the coordinates of some given cities (start place is Berlin), Berlin, Hamburg, Bielefeld, Dortmund, Essen, Bonn, Frankfurt, Trier and Paderborn, one route is: Berlin -> Hamburg -> Bielefeld -> Paderborn -> Dortmund -> Essen -> Bonn -> Trier -> Frankfurt



Solution 1:[1]

The following link provides the implementation of the travelling salesman problem: https://www.javatpoint.com/travelling-salesman-problem-in-java

I used the solution to get the route:

public class RouteHelper {
    public static ArrayList<Integer> route = null;
    
    public static double findHamiltonianCycle(double[][] distance, boolean[] visitCity, int currPos, int places, int count, double cost, double hamiltonianCycle) {
        if(route == null) {
            route = new ArrayList<>();
        }
        
        if (count == places && distance[currPos][0] > 0) { // an QJ: wieso bei 0? distance mit Start?
            hamiltonianCycle = Math.min(hamiltonianCycle, cost + distance[currPos][0]);
            route.add(currPos);
            return hamiltonianCycle;
        }
    
        // BACKTRACKING STEP
        for (int i = 0; i < places; i++) {
            if (visitCity[i] == false && distance[currPos][i] > 0) {
                // Mark as visited  
                visitCity[i] = true;
                hamiltonianCycle = findHamiltonianCycle(distance, visitCity, i, places, count + 1, cost + distance[currPos][i], hamiltonianCycle);  
    
                // Mark ith node as unvisited  
                visitCity[i] = false;  
            }
        }

        return hamiltonianCycle;
    }
}

In Main:

double[][] distances = new double [coordinatesList.size()][coordinatesList.size()];
            for(int j=0; j<coordinatesList.size();j++) {
                for(int k=0; k<coordinatesList.size();k++) {
                    distances[j][k] = RouteHelper.distanceBetweenTwoCoordinates(coordinatesList.get(j), coordinatesList.get(k));
                }
            }
            System.out.println(RouteHelper.findHamiltonianCycle(distances, new boolean[coordinatesList.size()], 0, coordinatesList.size(), 0, 0, 0));
            model.put("shortestRouteAddressesList", RouteHelper.route);

route.add(currPos); is on a false position: The route is a very long list, thousands of entries

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 beub