Dijkstra在java中的方法(逐步)

时间:2015-06-25 23:46:29

标签: java dijkstra

我在我的一个项目中使用dijkstra的方法,这里是所用类的完整代码:

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;
}

@Override
public String toString() {
    return name;
}

@Override
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 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 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;
}
}

我想要显示算法的所有步骤(步骤表),我不知道如何开始?有谁知道我这样做的方式?

0 个答案:

没有答案