java - 首都之间经过其他首都的最短路径

标签 java path nodes shortest-path

我正在尝试为大学作业开发一些代码,并且我有一个算法可以为我提供图中两个节点之间的最短路径。请注意,节点是有首都的国家。

谁能解释一下如何开发一种东西,通过一系列首都(国家)提供从 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;
}

谢谢。

最佳答案

问题描述:

给定一个具有顶点 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

关于java - 首都之间经过其他首都的最短路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58748130/

相关文章:

Java JSF : Editing rows in a dataTable tag

java - 第三次出现时用 null 替换子字符串

hadoop - 为什么hadoop丢失节点

java - 如何通过对 Infinispan 或 Hibernate Search 的 API 调用为单个项目编制索引?

java.util.Scanner 不返回提示符

windows - 如何调用QT_SCALE_FACTOR?

path - 用于获取所选文件的 POSIX 路径的 AppleScript,也在网络驱动器上

javascript - 解析 SVG 路径元素中的 d 属性

Java 节点交换

Node.js 代码不执行