简体   繁体   中英

Djikstra's Algorithm using Adjacency List and Priority queue in java

I am having trouble understand why my dijkstraShortestPath(int startVertex) function is not working properly. I am following pseudocode for my project, but I do not understand what I am doing wrong.

Only the walk for my start vertex is showing up for my algorithm.

I also have a DFS, but I am not sure if I should be using it in my dijkstraShortestPath method, and if I do, how do I implement it?

I think my issue is either in the "while loop" or the way I am initializing my priority queue named "pq".

Link to FULL code: https://www.dropbox.com/s/b848b9ts5lrfn01/Graph%20copy.java?dl=0

Link to pseudocode: https://www.dropbox.com/s/tyia0sr3t9r8snf/Dijkstra%27s%20Algorithm%20%281%29.docx?dl=0

Link to requirements: https://www.dropbox.com/s/rq8km8rp4jvyxvp/Project%202%20Description-1%20%282%29.docx?dl=0

Below is the code for my Dijkstra Algorithm.

public void dijkstraShortestPaths(int startVertex) {
        // Initialize VARS and Arrays
        int count = 0, start = startVertex;
        int[] d;
        int[] parent;
        d = new int[nVertices];
        parent = new int[nVertices];
        DistNode u;

        // 10000 is MAX/Infinity
        for (int i = 0; i < nVertices; i++) {
            parent[i] = -1;
            d[i] = 10000;
        }

        // Initialize Start vertex distance to 0
        d[startVertex] = 0;

        // Setup Priotiry Queue
        PriorityQueue<DistNode> pq = new PriorityQueue<DistNode>();

        for(int i = 0; i < adjList[start].size(); i++){
            pq.add(new DistNode(adjList[start].get(i).destVertex, adjList[start].get(i).weight));

        }
        System.out.print(pq);

        //
        while (count < nVertices && !pq.isEmpty()) {
            // remove DistNode with d[u] value
            u = pq.remove();

            count++;
            System.out.println("\n\nu.vertex: " + u.vertex);
            // for each v in adjList[u] (adjacency list for vertex u)
            for(int i = 0; i < adjList[u.vertex].size();i++){
                // v
                int v = adjList[u.vertex].get(i).destVertex;
                System.out.println("v = " + v);
                // w(u,v)
                int vWt = adjList[u.vertex].get(i).weight;
                System.out.println("vWt = " + vWt + "\n");

                if((d[u.vertex] + vWt) < d[v]){
                    d[v] = d[u.vertex] + vWt;
                    parent[v] = u.vertex;
                    pq.add(new DistNode(v,d[v]));
                }   
            }            
        }
        printShortestPaths(start, d, parent);
    }

The problem with your using a PriorityQueue is that the content of the the priority queue is unrelated to the content of the array d ; and the statement:

pq.add(new DistNode(v,d[v]));

Should replace any DistNode in pq with vertex v , otherwise, you may visit the same vertex multiple times.

I'm not sure that the PriorityQueue is the right tool for the job.

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