문제

Here is my problem (path Graph)

I would like to find shothest path from A to F, i read that i should use Dijkstra algorithm, and i tried to do this but i have problems with setting edges:

in 79 line i set edges http://pastebin.com/UAZiP7qb

It is good option to solve this case by this algorithm? What should i do?

I can't set edges correct:

public static void main(String[] args)
    {
        // mark all the vertices
        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");


        // set the edges and weight
        A.adjacencies = new Edge[]{ new Edge(B, 0) };
        B.adjacencies = new Edge[]{ new Edge(E, 2) };
        E.adjacencies = new Edge[]{ new Edge(F, 2) };
}
도움이 되었습니까?

해결책

You leave some adjancency lists as null. Check, before trying to iterate over them.

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

Next two lines added

    if (u.adjacencies == null)
        continue;

        // Visit each edge exiting u
        for (Edge e : u.adjacencies)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top