我正在编写一个程序来实现Dijkstra的算法(节点和图中每个其他节点之间的最短路径)。我希望用户给出的源节点作为输入读取为顶点(用户定义的数据类型)。
以下是我的计划:
import java.util.*;
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 DijExample
{
public static void computePaths(Vertex source) {
/*This function computes the shortest distance*/
}
public static List<Vertex> getShortestPathTo(Vertex target){
/*This function computes the shortest path*/
}
public static void main(String[] args){
in = new Scanner(System.in);
String sn;
Vertex v0 = new Vertex("S");
Vertex v1 = new Vertex("A");
Vertex v2 = new Vertex("B");
Vertex v3 = new Vertex("C");
Vertex v4 = new Vertex("D");
Vertex v5 = new Vertex("E");
Vertex v6 = new Vertex("F");
Vertex v7 = new Vertex("G");
Vertex v8 = new Vertex("H");
Vertex v9 = new Vertex("I");
Vertex v10 = new Vertex("J");
Vertex v11 = new Vertex("K");
Vertex v12 = new Vertex("L");
v0.adjacencies = new Edge[]{new Edge(v1,5),new Edge(v2,7),new Edge(v3,1),new Edge(v4,3),new Edge(v5,2),new Edge(v6,6)};
v1.adjacencies = new Edge[]{new Edge(v0,5),new Edge(v5,4),new Edge(v6,7)};
v2.adjacencies = new Edge[]{new Edge(v0,7),new Edge(v10,6),new Edge(v11,2)};
v3.adjacencies = new Edge[]{new Edge(v0,1), new Edge(v11,1)};
v4.adjacencies = new Edge[]{new Edge(v0,3),new Edge(v7,7)};
v5.adjacencies = new Edge[]{new Edge(v0,2),new Edge(v8,5)};
v6.adjacencies = new Edge[]{new Edge(v0,6),new Edge(v1,7),new Edge(v9,3)};
v7.adjacencies = new Edge[]{new Edge(v4,7)};
v8.adjacencies = new Edge[]{new Edge(v5,5)};
v9.adjacencies = new Edge[]{new Edge(v6,3)};
v10.adjacencies = new Edge[]{new Edge(v2,6),new Edge(v11,8),new Edge(v12,1)};
v11.adjacencies = new Edge[]{new Edge(v2,2),new Edge(v3,1),new Edge(v10,8),new Edge(v12,3)};
v12.adjacencies = new Edge[]{new Edge(v10,1),new Edge(v11,3)};
Vertex[] vertices = { v0,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12 };
System.out.println("Enter the source node");
sn = in.next();//Here sn is read as a string but I want to read it as a vertex
computePaths(sn);//Calling the function to calculate the shortest path with the source node (of type vertex)
for (Vertex v : vertices){
System.out.println("Distance to " + v + ": " + v.minDistance);
List<Vertex> path = getShortestPathTo(v);
System.out.println("Path: " + path);
}
}
}
答案 0 :(得分:0)
我假设您的问题是将用户输入转换为自定义类型,而不是算法本身。 琐碎的答案是改变你的:
sn = in.next();
要
Vertex vn = new Vertex (in.next());
这为你提供了一个新的顶点,但它并不在你构建的arry中,并且你没有一种简单的方法来查找是否存在匹配的顶点。你也会受到不好的投入。
更有用的机制可能是将您的顶点集合更改为某种Map
(可能是HashMap<String,Vertex>
),并在Vertex.name
上键入。
您可以像Vertices.add (v0.name, v0);
然后您可以检查用户是否指定了有效的顶点 - 返回读取字符串:
Vertex vn = null;
sn = in.next();
if (Vertices.contains(sn)) {
vn = Vertices.get(sn);
} else {
System.out.println ("That's not a vertex I know about");
System.exit(1);
}