Search code examples
javagraphdijkstrabreadth-first-searchvisited

Does Dijkstra algorithm keep track of visited nodes to find the shortest path?


I found this code for Dijkstra algorithm to find shortest path between two nodes in a weighted graph. what I see is that the code does not keep track of visited nodes. however it works fine for all inputs I tried. I added a line of code to keep track of visited nodes. It still works fine. I have commented out in this code. so is it a requirement to have the visited nodes or not? does it have any influence O

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>();
        //Set<Vertex> visited = new HashSet<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 (!visited.contains(u)){
        if (distanceThroughU < v.minDistance) {
            vertexQueue.remove(v);
            v.minDistance = distanceThroughU ;
            v.previous = u;
            vertexQueue.add(v);
                visited.add(u)
        //}
            }
        }
    }
}

    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)
    {
        Vertex v0 = new Vertex("Redvile");
    Vertex v1 = new Vertex("Blueville");
    Vertex v2 = new Vertex("Greenville");
    Vertex v3 = new Vertex("Orangeville");
    Vertex v4 = new Vertex("Purpleville");

    v0.adjacencies = new Edge[]{ new Edge(v1, 5),
                                 new Edge(v2, 10),
                               new Edge(v3, 8) };
    v1.adjacencies = new Edge[]{ new Edge(v0, 5),
                                 new Edge(v2, 3),
                                 new Edge(v4, 7) };
    v2.adjacencies = new Edge[]{ new Edge(v0, 10),
                               new Edge(v1, 3) };
    v3.adjacencies = new Edge[]{ new Edge(v0, 8),
                                 new Edge(v4, 2) };
    v4.adjacencies = new Edge[]{ new Edge(v1, 7),
                               new Edge(v3, 2) };
    Vertex[] vertices = { v0, v1, v2, v3, v4 };
        computePaths(v0);
        for (Vertex v : vertices)
    {
        System.out.println("Distance to " + v + ": " + v.minDistance);
        List<Vertex> path = getShortestPathTo(v);
        System.out.println("Path: " + path);
    }
    }
}

Solution

  • The code could have been simpler, but irrespective of that, Djikstra is greedy, so at each node, we try to find the node with the shortest path. Unless there are negative edges, then nodes that are already visited would already be populated with the shortest path, so naturally, the condition if (distanceThroughU < v.minDistance) will never be true for visited nodes.

    Regarding the run time complexity, there would be not much difference between your two implementations.