Java 返回Dijkstra的路径';s算法

Java 返回Dijkstra的路径';s算法,java,dijkstra,path-finding,Java,Dijkstra,Path Finding,所以,我一直在实现Dijkstra的算法,通过我生成的迷宫进行寻路(我知道你们中的一些人可能会认为a*之类的更好,但Dijkstra的算法非常适合我的需要),我遇到了一个小问题。我觉得我忽略了一些东西,但是当我返回从开始节点到结束节点的“路径”时,它返回算法所采用的整个搜索路径(它访问的每个节点),而不是到节点的路径 //Uses djikstra's algorithm to find the shortest path between two nodes //"vertices" is th

所以,我一直在实现Dijkstra的算法,通过我生成的迷宫进行寻路(我知道你们中的一些人可能会认为a*之类的更好,但Dijkstra的算法非常适合我的需要),我遇到了一个小问题。我觉得我忽略了一些东西,但是当我返回从开始节点到结束节点的“路径”时,它返回算法所采用的整个搜索路径(它访问的每个节点),而不是到节点的路径

//Uses djikstra's algorithm to find the shortest path between two nodes
//"vertices" is the global ArrayList in the class with all vertices in the graph.
@Override
public ArrayList<Vertex> pathfind(Vertex v1, Vertex v2) {
    ArrayList<Vertex> path = new ArrayList<Vertex>();
    ArrayList<Vertex> unvisited = new ArrayList<Vertex>();
    ArrayList<Vertex> visited = new ArrayList<Vertex>();

    if (!vertices.contains(v1) || !vertices.contains(v2)) {
        return path;
    }

    //initialize distances
    v1.setDistance(0);
    for(Vertex vert : vertices) {
        if(vert != v1) {
            vert.setDistance(Integer.MAX_VALUE);
        }
        unvisited.add(vert);
    }
    Vertex current = v1;

    //begin
    while (!unvisited.isEmpty()) {  
        //for all adjacent vertices that are unvisited
        for (Vertex v : current.adjacentVertices()) {
            if (!visited.contains(v)) {
                //if the distance of that vertex is greater than 
                //the added distance of the current node + the edge connecting the two
                int pathDist = current.getDistance() + findEdge(current, v).getElement(); 
                if (v.getDistance() > pathDist) {
                    //assign the new distance
                    v.setDistance(pathDist);
                }
            }
        }

        //remove the current node from the visited set and add it to visited
        visited.add(current);
        unvisited.remove(current);

        //add current node to the path
        path.add(current);

        //return if we found the destination
        if (current == v2)) {
            return path;
        }

        //else move to the lowest value node
        current = findSmallest(unvisited);

    }
    return path;

}
//使用djikstra算法查找两个节点之间的最短路径
//“顶点”是类中包含图形中所有顶点的全局ArrayList。
@凌驾
公共ArrayList路径查找(顶点v1,顶点v2){
ArrayList路径=新建ArrayList();
ArrayList unvisited=新建ArrayList();
ArrayList visited=新建ArrayList();
如果(!顶点.contains(v1)| |!顶点.contains(v2)){
返回路径;
}
//初始化距离
v1.设定距离(0);
对于(顶点顶点顶点:顶点){
如果(垂直!=v1){
垂直设置距离(整数最大值);
}
未访问。添加(垂直);
}
顶点电流=v1;
//开始
而(!unvisited.isEmpty()){
//对于未访问的所有相邻顶点
对于(顶点v:current.adjacentVertices()){
如果(!已访问。包含(v)){
//如果该顶点的距离大于
//当前节点的附加距离+连接两个节点的边
int pathDist=current.getDistance()+findEdge(current,v.getElement();
if(v.getDistance()>pathDist){
//指定新距离
v、 设置距离(路径距离);
}
}
}
//从访问集删除当前节点,并将其添加到访问集
已访问。添加(当前);
未访问。删除(当前);
//将当前节点添加到路径
添加路径(当前);
//如果我们找到目的地就返回
如果(当前=v2)){
返回路径;
}
//否则移动到最低值节点
当前=FindSalest(未访问);
}
返回路径;
}

我知道这肯定是件很明显的事情,但我的头撞到了墙上,任何帮助都将不胜感激。谢谢

您可能希望首先计算距离/使用algo将距离稳定到最小值,然后在图形上运行以获取路径的节点


由于
current=findsmalest(unvisited)
可能会从未连接的路径获取节点。

感谢大家的帮助,我最终制作了一个包含到前面节点的链接的哈希图,并从头开始遍历。

是否有理由使用
current.equals(v2)
而不是
current==v2
?这似乎是一种需要
==
的情况,因为您希望查看是否在同一个顶点,而不是顶点中的数据是否相等(如果您将
覆盖为等于
)。不过,我怀疑这是问题的原因。@ajb每个顶点都有一个坐标值,
.equals
检查坐标是否相等。你有一个很好的观点,但任何一个都可以。事实上,如果
顶点
等于
被覆盖,那么你就不能使用
包含
。哦,谢谢!我会纠正的。但是,路径的问题仍然存在,它可能是未更新的。每次将路径添加到已访问路径时,您都会将当前路径添加到该路径。你需要在这里添加一些条件。。。