0

I am implementing the code to get the shorted path in a directed graph as shown below using Dijkstra's algorithm?enter image description here

my question is

  1. how to define the adjacency list for a vertex? in my current code below, I have considered only the outgoing edges to the part of the adjacency list

  2. Does Dijkstra's alogithm fail if there is a cyclic pattern with in the graph? for example, ABD forms a cycle below

  3. if there is no outgoing edges from a vertex, then there is no shortest path from that vertex as the source, example: for the graph below, If I want to find the shortest path from F to A, there is none. should Dijsktra'a algorithm takes care of that?

I have implemented Dijsktra's algorithm, but I am not pasting that code here. After clarifying these doubts, I will post a seperate question on my problems with Dijkstra's implementation.

my current code for Vertex, Edge and Graph is below. As you can notice, I have defined the vertex and adjacency list for the above image. please pass your comments if the adjcacency list is correct. eg: vertex F has no adjacency list as there are no outgoing edges from it.

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 Graph { 
public static void main(String[] args) {
        Vertex A = new Vertex("A");
        Vertex B = new Vertex("B");
        Vertex C = new Vertex("C");
        Vertex D = new Vertex("D");
        Vertex E = new Vertex("E");
        Vertex F = new Vertex("F");
        Vertex G = new Vertex("G");

        A.adjacencies = new Edge[]{ new Edge(B, 1)};
        B.adjacencies = new Edge[]{ new Edge(C, 3), new Edge(D, 2)};
        C.adjacencies= new Edge[]{new Edge(D, 1),new Edge(E, 4)};
        D.adjacencies= new Edge[]{new Edge(E, 2),new Edge(A, 2) };
        E.adjacencies= new Edge[]{new Edge(F, 3) };
        //F.adjacencies= null;
        G.adjacencies= new Edge[]{new Edge(D, 1)};

    }
}
brain storm
  • 30,124
  • 69
  • 225
  • 393

1 Answers1

0
  1. Using only outgoing edges works.

  2. No. Good implementations of the algorithm doesn't have problems with cycles. In the algorithm, it's impossible to have a problem with cycles because the nodes are "eliminated" increasingly, and lots of edges are eliminated.

  3. Yes. The algorithm will take care of that. Before starting, you must be sure that all intermediate information is set correctly, for example, you need to be able to identify if nodes were removed. In the algorithm you need to select the node to start with, and if the node doesn't have adjacent nodes, then the other nodes (e.g. A), will have the previous attribute set to null. Would be easier to implement F.adjacencies = new Edge[0];

Javier
  • 376
  • 2
  • 12