A* Algorithm Java -


i have spent entire weekend playing around this. trying store nodes in priorityqueue data structure. astar function doesnt seem doing should. mind having look?

public void astar(node from, node to) {      priorityqueue<node> explorelist = new priorityqueue<node>();     arraylist<node> visited = new arraylist<node>();     arraylist<node> successors = new arraylist<node>();      node current = from;     system.out.println(current.getname());     while (current != to) {             successors = current.getconnected();             collections.sort(successors);             (node n : successors) {                     if (!visited.contains(n)) {                             explorelist.add(n);                     }                     (node n1 : successors) {                             if (n.fsum() > n1.fsum()) {                             explorelist.remove(n);                             explorelist.add(n1);                             }                           }             }             visited.add(current);             current = explorelist.remove();             system.out.println(current.getname());     } 

node class here

   public class node implements comparable {    private string name;    private int travdist;    private int straightdist;    private arraylist<arc> arcs;  /**  * constructor new node  *   * @param n  */    public node(string n, int atravdist, int astraightdist) {        name = n;        travdist = atravdist;        straightdist = astraightdist;         arcs = new arraylist<arc>();   }  /**  * adds new arc  *   * @param  * @param c  */ public void addarc(node to, int c) {     arcs.add(new arc(to, c)); }  /**  * gets list of connected nodes node  *   * @return  */ public arraylist<node> getconnected() {     arraylist<node> returndata = new arraylist<node>();     (arc : arcs) {         returndata.add(a.getnode());     }     return returndata; }  @override public int compareto(object o) {     //return name.compareto(((node) o).getname());     integer sum = ((node)o).fsum();     return sum.compareto(fsum()); }  public int fsum () {      return travdist + straightdist; }  /**  * gets name of node  *   * @return  */ public string getname() {     return name; }   } 

what doing not proper star algorithm.

collections.sort(successors); 

you shouldn't that. in star consider successor. neededn't worry order- priority queue take care of that. however, addibng line increase complexity of algorithm.

for (node n1 : successors) {   if (n.fsum() > n1.fsum()) {     explorelist.remove(n);     explorelist.add(n1);   }       } 

this entirely wrong. doing here is: add closest of successors. willl beam search beam of size 1, not star - keep them in.


Comments