当前位置: 首页 > 面试题库 >

Java-在距离加权图中的2点之间找到最短路径

郭云
2023-03-14
问题内容

我需要一种算法来找到地图上两点之间的最短路径,其中道路距离用数字表示。

提供的内容:开始城市A目的地城市Z

城市之间的距离清单:

A-B:10
F-K:23
R-M:8
K-O:40
Z-P:18
J-K:25
D-B:11
M-A:8
P-R:15

我以为我可以使用Dijkstra的算法,但是它找到所有目的地的最短距离。不只是一个

任何建议表示赞赏。


问题答案:

就像SplinterReality所说: There's no reason not to use Dijkstra's algorithm here.

下面的代码我从这里开始进行了昵称,并对其进行了修改以解决问题中的示例

import java.util.PriorityQueue;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

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 Dijkstra
{
    public static void computePaths(Vertex source)
    {
        source.minDistance = 0.;
        PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
        vertexQueue.add(source);

        while (!vertexQueue.isEmpty()) {
            Vertex u = vertexQueue.poll();

            // Visit each edge exiting u
            for (Edge e : u.adjacencies)
            {
                Vertex v = e.target;
                double weight = e.weight;
                double distanceThroughU = u.minDistance + weight;
                if (distanceThroughU < v.minDistance) {
                    vertexQueue.remove(v);

                    v.minDistance = distanceThroughU ;
                    v.previous = u;
                    vertexQueue.add(v);
                }
            }
        }
    }

    public static List<Vertex> getShortestPathTo(Vertex target)
    {
        List<Vertex> path = new ArrayList<Vertex>();
        for (Vertex vertex = target; vertex != null; vertex = vertex.previous)
            path.add(vertex);

        Collections.reverse(path);
        return path;
    }

    public static void main(String[] args)
    {
        // mark all the vertices 
        Vertex A = new Vertex("A");
        Vertex B = new Vertex("B");
        Vertex D = new Vertex("D");
        Vertex F = new Vertex("F");
        Vertex K = new Vertex("K");
        Vertex J = new Vertex("J");
        Vertex M = new Vertex("M");
        Vertex O = new Vertex("O");
        Vertex P = new Vertex("P");
        Vertex R = new Vertex("R");
        Vertex Z = new Vertex("Z");

        // set the edges and weight
        A.adjacencies = new Edge[]{ new Edge(M, 8) };
        B.adjacencies = new Edge[]{ new Edge(D, 11) };
        D.adjacencies = new Edge[]{ new Edge(B, 11) };
        F.adjacencies = new Edge[]{ new Edge(K, 23) };
        K.adjacencies = new Edge[]{ new Edge(O, 40) };
        J.adjacencies = new Edge[]{ new Edge(K, 25) };
        M.adjacencies = new Edge[]{ new Edge(R, 8) };
        O.adjacencies = new Edge[]{ new Edge(K, 40) };
        P.adjacencies = new Edge[]{ new Edge(Z, 18) };
        R.adjacencies = new Edge[]{ new Edge(P, 15) };
        Z.adjacencies = new Edge[]{ new Edge(P, 18) };


        computePaths(A); // run Dijkstra
        System.out.println("Distance to " + Z + ": " + Z.minDistance);
        List<Vertex> path = getShortestPathTo(Z);
        System.out.println("Path: " + path);
    }
}

上面的代码产生:

Distance to Z: 49.0
Path: [A, M, R, P, Z]


 类似资料:
  • 我需要一个算法来找到地图上两点之间的最短路径,其中道路距离由一个数字表示。 给出的内容:开始城市A目的地城市Z 城市间距离列表: A-B: 10 F-K: 23 R-M: 8 K-O: 40 Z-P: 18 J-K: 25 D-B: 11 M-A: 8 P-R: 15 我想我可以用Dijkstra的算法,但是它能找到到所有目的地的最短距离。不仅仅是一个。 如有任何建议,我们将不胜感激。

  • 在无向赋权图中寻找两个顶点之间的最短路径。还已知权重是小于log(logV)的整数,其中V是顶点的数量。使用贝尔曼-福特或Dijkstra算法很容易解决,但是有没有什么算法可以更快地解决呢? 到目前为止,我一直在考虑使用BFS,将权重大于1的边划分成两个权重为1的边,但如果V是大数,就不是一个好主意。不,这不是我的家庭作业,我只是在想。

  • 我在使最短路径算法正常工作时遇到问题。我有一个20 x 20的2d数组,其中包含表示城市之间道路的边权重。我没有得到城市间最短路径的正确结果。如有任何帮助,我们将不胜感激。 我的2d阵列看起来像:X轴=城市1-20,y轴=城市1-20。 这是我到目前为止的代码: 对于城市1和2之间的最短路径,我的程序给出了总距离276。经历19、5、16、11 正确的解决方案是总距离225。路径应该19, 5,

  • 问题内容: 假设我有x1,y1,还有x2,y2。 我如何找到它们之间的距离?这是一个简单的数学函数,但是此在线代码段吗? 问题答案: dist = sqrt( (x2 - x1)2 + (y2 - y1)2 ) 正如其他人指出的那样,您也可以使用等效的内置函数:

  • 问题内容: 我需要有效地计算给定数组中每个点到另一个数组中每个其他点的欧几里德 加权 距离。这是我拥有的代码,可以正常工作: 最终列表包含的子列表数量与第一个数组中的点数量相同,每个元素中的元素数量与第二个数组中的点数量相同(加权距离)。 我想知道是否有一种方法可以提高此代码的效率,也许使用cdist函数,因为当数组中有很多元素(在我的情况下,它们具有)并且必须检查时,此过程会变得非常昂贵许多数组