【问题标题】:Shortest Path between capitals passing through other capitals首都之间通过其他首都的最短路径
【发布时间】:2019-11-07 11:47:40
【问题描述】:

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

谁能解释我如何开发一些东西,让我通过首都(国家)列表从 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;
}

谢谢。

【问题讨论】:

  • 在开始写代码之前,你对这个问题做了哪些研究?
  • @ct_ 我有一个大学作业告诉我创建一个包含国家和边界的图表,然后开发这个算法。他们还给了我算法,它给了我最排序的路径以及 A 点和 B 点之间的距离。
  • 鉴于这些信息,您随后做了哪些研究?
  • 我发现了一种叫做 floyd warshall 算法的东西,但是在这个时间范围内,我现在无法详细说明矩阵并实现这个算法,因为我已经有了处理图表的东西。

标签: java path nodes shortest-path


【解决方案1】:

问题描述:

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

【讨论】:

  • 非常感谢。你能解释一下“如果 verticesToBeVisited 包含 Vn”中的“包含”是什么意思吗?“继续”是什么意思?
  • “包含”是指“包含”的过去式。这意味着您需要存储verticesToBeVisited 是否包含Vn,并在以后使用此布尔变量。在大多数语言中,Continue 是一个实际的关键字。这意味着“跳过循环的这个迭代”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-27
相关资源
最近更新 更多