【发布时间】:2020-10-06 16:32:30
【问题描述】:
我确实有一个关于使用 Dijkstra 检查最短路径是否严格单调的问题。 这是我检查下一个未访问的最近节点的函数:
private static Node nearestUnvisitedNode(HashMap<Node, Integer> shortestPathMap) {
int shortestDistance = Integer.MAX_VALUE;
Node nearest = null;
for (Node node : Graph.nodes) {
if (node.isVisited()) {
continue;
}
int currentDistance = shortestPathMap.get(node);
if (currentDistance == Integer.MAX_VALUE) {
continue;
}
if (currentDistance < shortestDistance) {
shortestDistance = currentDistance;
nearest = node;
}
}
return nearest;
}
连接到每个节点的边被排序并存储在 LinkedList<Edge> edges; 中
shortestPathMap 是一个实用程序,用于存储从起始节点到结束节点的整个路径。
我应该对nearestUnvisitedNode 方法应用哪些更改以确保路径权重严格增加或减少?
谢谢!
【问题讨论】:
-
您是要检查路径是否是单调的,还是要强制搜索以选择单调的下一条边?
-
@c0der 最终结果应该是严格单调的最短路径
-
你能把剩下的相关代码贴出来吗?
-
每条边都有源、目的和权重,每个节点都有一个连接边列表,图包含节点的信息。
标签: java algorithm data-structures computer-science dijkstra