我正在尝试实现Dijkstra算法,该算法可以找到起始节点和终止节点之间的最短路径。在到达终点之前,存在一些“必经”中间节点(一个以上),例如2个或3个必经节点,这些节点必须在到达终点之前通过。
如果我有一个必经节点,我找到的解决方案是从必经节点到目的地和从必经节点到起始节点找到两条不同的路径。
我已经没有想法如何实现这样的算法了。有什么建议吗?
谢谢。
如果我有一个必经节点,我找到的解决方案是从必经节点到目的地和从必经节点到起始节点找到两条不同的路径。
我已经没有想法如何实现这样的算法了。有什么建议吗?
谢谢。
List<Node> closestPathFromOrigin = null;
double maxD = Double.POSITIVE_INFINITY;
double _distance = 0;
int temp1 = 0;
List<Node> referencePath = new ArrayList<>();
boolean check = false;
Node startNode = null;
public List<Node> recursion(ArrayList<Node> points, ArrayList<Node> intermediatePoints) {
if (!check) {
System.out.println("--- DATA ---");
System.out.println("Intermediate points: " + intermediatePoints);
System.out.println("points: " + points.get(0).lat + " " + points.get(1).lat);
System.out.println("--Find the nearest intermediate point from the start point of driver--");
startNode = points.get(0);
System.out.println("Start point of driver: " + startNode.lat + " " + startNode.lon);
for (int i = 0; i < intermediatePoints.size(); i++) {
List<Node> _path = dijkstra(startNode, intermediatePoints.get(i));
_distance = 0;
for (int j = 0; j < _path.size() - 1; j++) {
_distance += calculateDistance(_path.get(j), _path.get(j + 1));
}
if (_distance < maxD) {
maxD = _distance;
closestPathFromOrigin = _path;
temp1 = i;
}
}
System.out.println("NearestPoint from driver's origin: " + intermediatePoints.get(temp1));
referencePath.addAll(closestPathFromOrigin);
startNode = intermediatePoints.get(temp1);
System.out.println("New StartNode: the nearestPoint from driver's origin: " + startNode.lat + " " + startNode.lon);
check = true;
intermediatePoints.remove(intermediatePoints.get(temp1));
System.out.println("New Intermediate points: " + intermediatePoints);
System.out.println("Intermediate points empty? No -> recursion, Yes -> stop");
if (!intermediatePoints.isEmpty()) {
System.out.println("Recursion!!! with new data of: intermediatePoints: " + intermediatePoints);
recursion(points, intermediatePoints);
} else {
System.out.println("Stop");
return referencePath;
}
} else {
System.out.println("Recursion: startNode: " + startNode.lat + " " + startNode.lon);
for (int i = 0; i < intermediatePoints.size(); i++) {
if (intermediatePoints.size() > 1) {
System.out.println("From the new start point to the next nearest intermediate points if more than one points");
List<Node> _path = dijkstra(startNode, intermediatePoints.get(i));
_distance = 0;
for (int j = 0; j < _path.size() - 1; j++) {
_distance += calculateDistance(_path.get(j), _path.get(j + 1));
}
if (_distance < maxD) {
maxD = _distance;
closestPathFromOrigin = _path;
temp1 = i;
}
referencePath.addAll(closestPathFromOrigin);
startNode = intermediatePoints.get(temp1);
check = true;
intermediatePoints.remove(intermediatePoints.get(temp1));
if (!intermediatePoints.isEmpty()) {
recursion(points, intermediatePoints);
} else {
return referencePath;
}
} else {
System.out.println("From the new start point to the next nearest intermediate points if just one point");
List<Node> _path = dijkstra(startNode, intermediatePoints.get(i));
//Collections.reverse(_path);
referencePath.addAll(_path);
}
if (i == intermediatePoints.size() - 1) {
System.out.println("Last Entry in intermediate points - find path to destination: " + points.get(1).lat + " " + intermediatePoints.get(i));
//List<Node> _path1 = dijkstra(points.get(1), intermediatePoints.get(i));
List<Node> _path1 = dijkstra(intermediatePoints.get(i), points.get(1));
Collections.reverse(_path1);
referencePath.addAll(_path1);
// referencePath.addAll(_path2);
}
}
}
return referencePath;
}