Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 首都间通过其他首都的最短路径_Java_Path_Nodes_Shortest Path - Fatal编程技术网

Java 首都间通过其他首都的最短路径

Java 首都间通过其他首都的最短路径,java,path,nodes,shortest-path,Java,Path,Nodes,Shortest Path,我正试图为一个大学的工作开发一些代码,我有一个算法,可以给我一个图中两个节点之间的最短路径。请注意,节点是具有资本的国家 有谁能解释一下,我怎样才能开发出一种东西,让我通过一系列的首都(国家)找到从A国到B国的最短路径 我已经实现了一种方法,该方法还提供了两个地理点之间的距离 我最初的想法是根据各国首都到A国的距离对其进行排序,然后将A国到列表第一个国家、列表第一个国家和列表第三个国家之间最短路径的所有距离相加,依此类推。这显然是不对的 public double shortestPat

我正试图为一个大学的工作开发一些代码,我有一个算法,可以给我一个图中两个节点之间的最短路径。请注意,节点是具有资本的国家

有谁能解释一下,我怎样才能开发出一种东西,让我通过一系列的首都(国家)找到从A国到B国的最短路径

我已经实现了一种方法,该方法还提供了两个地理点之间的距离

我最初的想法是根据各国首都到A国的距离对其进行排序,然后将A国到列表第一个国家、列表第一个国家和列表第三个国家之间最短路径的所有距离相加,依此类推。这显然是不对的

    public double shortestPathCapitals2(List<String> capitais, Pais pOrig, Pais pDest) {
    double dist = 0;
    LinkedList<Pais> shortPath = new LinkedList<Pais>();
    LinkedList<String> temp = new LinkedList<>(capitais);

    temp.addFirst(pOrig.getCapital());
    temp.addLast(pDest.getCapital());

    Collections.sort(temp, (c1, c2) -> (int) (distance(pOrig, shortestPathCapitals2(c2)) - distance(pOrig, obterPaisPorCapital(c1))));

    for (int i = 0; i < temp.size() - 1; i++) {
        Pais p1 = obterPaisPorCapital(temp.get(i));
        Pais p2 = obterPaisPorCapital(temp.get(i + 1));
        dist += shortestPath(p1, p2, shortPath);
        shortPath.clear();
    }

    return dist;
}
public double ShortestPathCapitals 2(列出Capital、Pais pOrig、Pais pDest){
双距离=0;
LinkedList shortPath=新建LinkedList();
LinkedList temp=新的LinkedList(大写);
临时addFirst(pOrig.getCapital());
临时addLast(pDest.getCapital());
排序(temp,(c1,c2)->(int)(距离(pOrig,最短路径资本2(c2))-距离(pOrig,obterPaisPorCapital(c1)));
对于(int i=0;i

谢谢。

问题描述:

function findPath(startVertex, endVertex, verticesToBeVisited, currentPath)

    // check if we have reached the destination
    if startVertex == endVertex:

          /*
           * there are multiple ways of reaching the destination
           * calculate the length of the past (also called the cost)
           * if the cost is lower than the current minimum, store the path
           */
          cost = calculateCost(currentPath)
          if cost  < currentMinCost:
              currentMinCost = cost
              currentMinPath = currentPath            

    else:

        /*
         * if we have not reached the destination
         * we need to try all possible next hops
         * this algorithm uses recursion to do so
         */
        for every vertex Vn that is a neighbour of startVertex:

            /*
             * this check prevents us from going
             * Paris --> Rome --> Paris --> Rome (endlessly)
             */
            if currentPath contains Vn:
                 continue

            // add the next hop to our path
            currentPath += Vn

            // if this vertex needed to be visit, cross it out in the list
            if verticesToBeVisited contains Vn:
                verticesToBeVisited -= Vn

            // recursion
            findPath(Vn, endVertex, verticesToBeVisited, currentPath)

            // clean up
            if verticesToBeVisited contained Vn:
                verticesToBeVisited += Vn

            currentPath -= Vn
给定一个顶点为V,边为E的图。 我们希望在Va和Vb之间找到路径P,以便:

  • 路径包含{V0,V1,…}(V的一些子集)
  • P中边上的权重之和是最小的
伪代码:

function findPath(startVertex, endVertex, verticesToBeVisited, currentPath)

    // check if we have reached the destination
    if startVertex == endVertex:

          /*
           * there are multiple ways of reaching the destination
           * calculate the length of the past (also called the cost)
           * if the cost is lower than the current minimum, store the path
           */
          cost = calculateCost(currentPath)
          if cost  < currentMinCost:
              currentMinCost = cost
              currentMinPath = currentPath            

    else:

        /*
         * if we have not reached the destination
         * we need to try all possible next hops
         * this algorithm uses recursion to do so
         */
        for every vertex Vn that is a neighbour of startVertex:

            /*
             * this check prevents us from going
             * Paris --> Rome --> Paris --> Rome (endlessly)
             */
            if currentPath contains Vn:
                 continue

            // add the next hop to our path
            currentPath += Vn

            // if this vertex needed to be visit, cross it out in the list
            if verticesToBeVisited contains Vn:
                verticesToBeVisited -= Vn

            // recursion
            findPath(Vn, endVertex, verticesToBeVisited, currentPath)

            // clean up
            if verticesToBeVisited contained Vn:
                verticesToBeVisited += Vn

            currentPath -= Vn
函数findPath(startVertex、endVertex、verticesToBeVisited、currentPath)
//检查我们是否已到达目的地
如果startVertex==endVertex:
/*
*到达目的地有多种方式
*计算过去的时间长度(也称为成本)
*如果成本低于当前最小值,请存储路径
*/
成本=计算成本(当前路径)
如果成本<当前最小成本:
当前最小成本=成本
currentMinPath=currentPath
其他:
/*
*如果我们还没有到达目的地
*我们需要尝试所有可能的下一跳
*该算法使用递归来实现这一点
*/
对于startVertex相邻的每个顶点Vn:
/*
*这张支票使我们不能去
*巴黎-->罗马-->巴黎-->罗马(无休止)
*/
如果currentPath包含Vn:
持续
//将下一跳添加到我们的路径
currentPath+=Vn
//如果需要访问此顶点,请在列表中划掉它
如果VerticesToBevisite包含Vn:
verticesToBeVisited-=Vn
//递归
findPath(Vn、endVertex、verticesToBeVisited、currentPath)
//清理
如果VerticesToBevisite包含Vn:
verticesToBeVisited+=Vn
currentPath-=Vn

在开始编写代码之前,您对这个问题做了哪些研究?@ct_uu我有一个大学任务,告诉我创建一个包含国家和边界的图表,然后开发这个算法。他们还给了我一个算法,该算法给出了分拣路径以及A点和B点之间的距离。根据这些信息,你后来做了什么研究?我发现了一种叫做floyd warshall算法的算法,但在这个时间范围内,我现在无法详细说明一个矩阵并实现这个算法,因为我已经有了处理图形的方法。非常感谢。你能解释一下“if vertices to evisited contained Vn”中“contained”是什么意思吗?contained是指“contains”的过去时。这意味着您需要存储verticesToBeVisited是否包含Vn,并在以后使用此布尔变量。在大多数语言中,Continue是一个实际的关键字。它的意思是“跳过循环的这个迭代”