我需要一个算法来找到地图中两点之间的最短路径,其中路程距离由一个数字表示。
已知信息: 起点城市A 目的地城市Z
城市之间距离列表:
A - B : 10
F - K : 23
R - M : 8
K - O : 40
Z - P : 18
J - K : 25
D - B : 11
M - A : 8
P - R : 15
我考虑使用Dijkstra算法,但它会找到所有目的地的最短距离,而不仅仅是一个。
欢迎提出任何建议。
我需要一个算法来找到地图中两点之间的最短路径,其中路程距离由一个数字表示。
已知信息: 起点城市A 目的地城市Z
城市之间距离列表:
A - B : 10
F - K : 23
R - M : 8
K - O : 40
Z - P : 18
J - K : 25
D - B : 11
M - A : 8
P - R : 15
我考虑使用Dijkstra算法,但它会找到所有目的地的最短距离,而不仅仅是一个。
欢迎提出任何建议。
就像SplinterReality所说的:在这里使用Dijkstra算法没有理由不用。
下面的代码是我从这里借鉴并修改以解决问题中的示例。
import java.util.PriorityQueue;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
class Vertex implements Comparable<Vertex>
{
public final String name;
public Edge[] adjacencies;
public double minDistance = Double.POSITIVE_INFINITY;
public Vertex previous;
public Vertex(String argName) { name = argName; }
public String toString() { return name; }
public int compareTo(Vertex other)
{
return Double.compare(minDistance, other.minDistance);
}
}
class Edge
{
public final Vertex target;
public final double weight;
public Edge(Vertex argTarget, double argWeight)
{ target = argTarget; weight = argWeight; }
}
public class Dijkstra
{
public static void computePaths(Vertex source)
{
source.minDistance = 0.;
PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
vertexQueue.add(source);
while (!vertexQueue.isEmpty()) {
Vertex u = vertexQueue.poll();
// Visit each edge exiting u
for (Edge e : u.adjacencies)
{
Vertex v = e.target;
double weight = e.weight;
double distanceThroughU = u.minDistance + weight;
if (distanceThroughU < v.minDistance) {
vertexQueue.remove(v);
v.minDistance = distanceThroughU ;
v.previous = u;
vertexQueue.add(v);
}
}
}
}
public static List<Vertex> getShortestPathTo(Vertex target)
{
List<Vertex> path = new ArrayList<Vertex>();
for (Vertex vertex = target; vertex != null; vertex = vertex.previous)
path.add(vertex);
Collections.reverse(path);
return path;
}
public static void main(String[] args)
{
// mark all the vertices
Vertex A = new Vertex("A");
Vertex B = new Vertex("B");
Vertex D = new Vertex("D");
Vertex F = new Vertex("F");
Vertex K = new Vertex("K");
Vertex J = new Vertex("J");
Vertex M = new Vertex("M");
Vertex O = new Vertex("O");
Vertex P = new Vertex("P");
Vertex R = new Vertex("R");
Vertex Z = new Vertex("Z");
// set the edges and weight
A.adjacencies = new Edge[]{ new Edge(M, 8) };
B.adjacencies = new Edge[]{ new Edge(D, 11) };
D.adjacencies = new Edge[]{ new Edge(B, 11) };
F.adjacencies = new Edge[]{ new Edge(K, 23) };
K.adjacencies = new Edge[]{ new Edge(O, 40) };
J.adjacencies = new Edge[]{ new Edge(K, 25) };
M.adjacencies = new Edge[]{ new Edge(R, 8) };
O.adjacencies = new Edge[]{ new Edge(K, 40) };
P.adjacencies = new Edge[]{ new Edge(Z, 18) };
R.adjacencies = new Edge[]{ new Edge(P, 15) };
Z.adjacencies = new Edge[]{ new Edge(P, 18) };
computePaths(A); // run Dijkstra
System.out.println("Distance to " + Z + ": " + Z.minDistance);
List<Vertex> path = getShortestPathTo(Z);
System.out.println("Path: " + path);
}
}
上述代码生成:
Distance to Z: 49.0
Path: [A, M, R, P, Z]
A --> Z
到Z的距离:49.0
路径:[A,M,R,P,Z]
B --> Z
到Z的距离:49.0
路径:[A,M,R,P,Z]
两者结果相同?我不确定为什么。你能帮忙检查一下吗? - Anoop Chandrika HarisudhanNairAMRPZ
之外没有其他可行的从 A 到 Z 的方式,我尝试了你的解决方案,但在一个更加复杂的图形上不能正常工作。 - henok估计sanjan:
Dijkstra算法的思想是按顺序探索图中的所有节点。该算法存储一个优先队列,其中节点根据从起点到该节点的成本排序,每次算法迭代执行以下操作:
确实,该算法计算起点(在您的情况下为A)与所有其他节点之间的路径成本,但当它到达目标(在您的示例中为Z)时,您可以停止算法的探索。此时,您已经知道了A和Z之间的成本以及连接它们的路径。
我建议您使用实现此算法的库,而不是编写自己的代码。在Java中,您可以看一下Hipster库,它具有非常友好的方式来生成图形并开始使用搜索算法。
这里有一个如何定义图形并使用Hipster进行Dijstra的示例。
// Create a simple weighted directed graph with Hipster where
// vertices are Strings and edge values are just doubles
HipsterDirectedGraph<String,Double> graph = GraphBuilder.create()
.connect("A").to("B").withEdge(4d)
.connect("A").to("C").withEdge(2d)
.connect("B").to("C").withEdge(5d)
.connect("B").to("D").withEdge(10d)
.connect("C").to("E").withEdge(3d)
.connect("D").to("F").withEdge(11d)
.connect("E").to("D").withEdge(4d)
.buildDirectedGraph();
// Create the search problem. For graph problems, just use
// the GraphSearchProblem util class to generate the problem with ease.
SearchProblem p = GraphSearchProblem
.startingFrom("A")
.in(graph)
.takeCostsFromEdges()
.build();
// Search the shortest path from "A" to "F"
System.out.println(Hipster.createDijkstra(p).search("F"));
您只需要用自己的图形定义替换原来的定义,然后按照示例实例化算法即可。
希望这有所帮助!
维护一个节点列表,按距离从起始节点排序。一开始,只有起始节点在列表中。
在你到达目的地之前:访问距离起始节点最近的节点,这将是你排序列表中的第一个节点。当你访问一个节点时,将其所有相邻的未访问过的节点添加到列表中。重复此过程!
感谢Lars Vogel提供的帮助