简体   繁体   中英

Dijkstra's algorithm in java

I'm implementing Dijkstra's algorithm in one of my projects but when i pass those points :

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");


// set the edges and weight
A.adjacencies = new Edge[]{ new Edge(B, 6) };
A.adjacencies = new Edge[]{ new Edge(D, 8) };

B.adjacencies = new Edge[]{ new Edge(F, 5) };
D.adjacencies = new Edge[]{ new Edge(F, 3) };
B.adjacencies = new Edge[]{ new Edge(A, 6) };
D.adjacencies = new Edge[]{ new Edge(A, 8) };

F.adjacencies = new Edge[]{ new Edge(B, 5) };
F.adjacencies = new Edge[]{ new Edge(D, 3) };
F.adjacencies = new Edge[]{ new Edge(K, 40) };

K.adjacencies = new Edge[]{ new Edge(F, 40) };

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

the algo's giving me : Distance to K: Infinity where's the problem ?

this's the full code of the algorithm :

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

any ideas why the distance = infinity ?

Here you overwrite the field's value. When you assign a new value of Edge[] , the old one gets deleted.
Did you intend to add instead?

B.adjacencies = new Edge[]{ new Edge(F, 5) };
D.adjacencies = new Edge[]{ new Edge(F, 3) };
B.adjacencies = new Edge[]{ new Edge(A, 6) };
D.adjacencies = new Edge[]{ new Edge(A, 8) };

To assign several Edges to each Vertex use array initializers with several elements:

A.adjacencies = new Edge[]{ new Edge(B, 6), new Edge(D, 8) };
B.adjacencies = new Edge[]{ new Edge(F, 5), new Edge(A, 6) };
D.adjacencies = new Edge[]{ new Edge(F, 3), new Edge(A, 8) };
F.adjacencies = new Edge[]{ new Edge(B, 5), new Edge(D, 3), new Edge(K, 40)};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM