How to implement Dijkstra's Algorithm to find the shortest path in Java -


i absolutely confused on do. i'm trying code off of pseudo code wikipedia has on dijkstra's priority queues, i'm having tough time making adjustments fit need find. (incomplete) code far, , appreciated.

public int dodijkstras (string startvertexname, string endvertexname, arraylist< string > shortestpath) {         priorityqueue<qentry> q = new priorityqueue<qentry>();         int cost = 0;         int newcost;         qentry pred = null;         (string s : this.getvertices()) {             if (!s.equals(startvertexname)) {                 cost = integer.max_value;                 pred = null;             }             q.add(new qentry(s, cost, pred, adjacencymap.get(s)));         }          while (!q.isempty()) {             qentry curr = getmin(q);             (string s : curr.adj.keyset()) {                 newcost = curr.cost + this.getcost(curr.name, s);                 qentry v = this.getvert(q, s);                 if (newcost < v.cost) {                     v.cost = newcost;                     v.pred = curr;                     if (!q.contains(curr)) {                         q.add(curr);                     }                 }             }         }     }      private qentry getmin(priorityqueue<qentry> q) {         qentry min = q.peek();         (qentry temp : q) {             if (min.cost > temp.cost) {                 min = temp;             }         }         return min;     }      private qentry getvert(priorityqueue<qentry> q, string s) {         (qentry temp : q) {             if (temp.name.equals(s)) {                 return temp;             }         }         return null;     }      class qentry {         string name;         int cost;         qentry pred;         treemap<string, integer> adj;          public qentry(string name, int cost, qentry pred, treemap<string, integer> adj) {             this.name = name;             this.cost = cost;             this.adj = adj;             this.pred = pred;         }     } 

you overlooking important part of algorithm: when stop.

the pseudocode on wikipedia variation on dijkstra's algorithm computes shortest path start node every node connected it. commentary following big pseudocode block explains how modify algorithm find path specific target, , after shorter block explaining how extract paths.

in english, though, you're processing priority queue, need watch target element being 1 selected. when (if ever) is, know no shorter path can discovered 1 having cost recorded in target's queue entry, , represented (in reverse order) entry , chain of predecessors. fill path list walking chain of predecessors, , return value recorded in target queue entry.

note, however, in code, in event start , target vertexes not connected in graph (including if target not in graph @ all), drain queue , fall out bottom of while loop without ever reaching target. have decide path list , return in case.

note, too, code appears have several errors, among them:

  • in event start vertex name not first 1 in iteration order of this.getvertices(), queue entry not initialized cost 0, , not first element chosen queue.
  • if specified start vertex not in graph @ code run, , may emit path, output in case bogus.
  • your queue elements (type qentry) not have natural order; create priorityqueue elements have such type, must provide comparator defines relative priorities.
  • you using priority queue plain list. in not make code produce wrong results, does increase asymptotic complexity.
  • be aware, however, if use standard priorityqueue as priority queue, must never modify enqueued object in way change order relative other enqueued object; instead, remove queue first, modify it, enqueue again.

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -