title: A-Star Algorithm is running like Dijkstra and delivers a wrong result
body: <p>I'm trying to implement A-Star in Java based on OSM Data. My problem is that my implementation is not working correctly. First of all the path is not the shortest. Second the closedlist contains more 1/3 more nodes in the end as Dijkstra. Thats actuall not that what I expected. </p>
  
  <p>Here is my A-Star code which is based on <a href="http://en.wikipedia.org/wiki/A-star_algorithm" rel="nofollow">Wikipedia Pseudocode</a></p>
  
  <pre><code>public Object[] executeAstar(ArrayList&lt;Arclistentry&gt; data, NodeD start, NodeD dest,long[] nodenur)
  {
      openlist = new PriorityQueue&lt;NodeD&gt;(1,comp);
      closedlist.clear();
      openlist.offer(start);
      start.setg(0);
      start.seth(calccost(start, dest));
      start.setf(start.getg()+start.geth());
      while(!openlist.isEmpty())
      {
          NodeD currentnode = openlist.poll();
          if(currentnode.getnodenumber() == dest.getpredessor())
          {
              closedlist.add(currentnode);
              return drawway(closedlist, start, dest);
          }
          closedlist.add(currentnode);
          ArrayList&lt;Arclistentry&gt; entries = neighbors.get((int)currentnode.getnodenumber()-1);
          for(Arclistentry aentry:entries)
          {
              NodeD successor = new NodeD(aentry.getnode(),aentry.getstart(), aentry.getcoorddest());
                  float tentative_g = currentnode.getg()+calccost(currentnode,successor);//+aentry.getcost();
                  if(contains(successor, closedlist))
                  {
                      continue;
                  }
                  if((contains(successor,openlist))&amp;&amp; tentative_g &gt;= aentry.getcost())
                  {
                      continue;
                  }
  
                          if(!contains(successor, openlist))
                          {
                              successor.setpredessor(currentnode.getnodenumber());
                              successor.setg(tentative_g);
                              successor.seth(calccost(successor, dest));
                              successor.setf(successor.getg()+successor.geth());
                              openlist.offer(successor);
                          }
                          else
                          {
                              openlist.remove(successor);
                              successor.setpredessor(currentnode.getnodenumber());
                              successor.setg(tentative_g);
                              successor.seth(calccost(successor, dest));
                              successor.setf(successor.getg()+successor.geth());
                              openlist.offer(successor);
                          }
          }
      }
      return drawway(closedlist,start, dest);
  }
  </code></pre>
  
  <p>My Heuristics will be calculated by using the euclidian distance. But to consider also the cost of the node, the costs are multiplied with the heuristics result. My Data structure contains the following:</p>
  
  <pre><code>private long nodenumber;
  private long predessor;
  private float label;
  private float f;
  private float g;
  private float h;
  private double[] coord = new double[2];
  
  public NodeD(long nodenr, long predessor, double[] coor)
  {
      this.nodenumber = nodenr;
      this.predessor = predessor;
      this.coord = coor;
  }
  public NodeD(long nodenr, long predessor, float label)
  {
      this.nodenumber = nodenr;
      this.predessor = predessor;
      this.label = label;
  }
  </code></pre>
  
  <p>and for the arclist I use the following:</p>
  
  <pre><code>private long start;
  private long dest_node;
  private float cost_;
  private double[]coordstart = new double[2];
  private double[]coorddest = new double[2];
  </code></pre>
  
  <p>Contains Function for Priority Queue:</p>
  
  <pre><code>public boolean contains(NodeD o, PriorityQueue&lt;NodeD&gt; al)
   {
       Iterator&lt;NodeD&gt; e = al.iterator();
       if (o==null)
       {
            while (e.hasNext())
            {
               if (e.next()==null)
               {
                   return true;
               }
            }
       }
       else
       {
           while (e.hasNext())
           {
               NodeD t = e.next();
               if(t.equals(null))
               {
                   return false;
               }
               if (((o.getnodenumber()==t.getnodenumber()) &amp; (o.getpredessor()==t.getpredessor()))||(o.getnodenumber()==t.getpredessor() &amp; o.getpredessor()==t.getnodenumber()))
               {
                   return true;
               }
           }
               return false;
       }
       return false;
   }
  </code></pre>
  
  <p>and contains for ArrayList (because it was not detecting right with the ArrayList.contains function</p>
  
  <pre><code>public boolean contains(NodeD o, ArrayList&lt;NodeD&gt; al) {
             return indexOf(o,al) &gt;= 0;
         }
  
  public int indexOf(NodeD o, ArrayList&lt;NodeD&gt; al) {
          if (o == null) {
              for (int i = 0; i &lt; al.size(); i++)
                  if (al.get(i)==null)
                      return i;
          } else {
                     for (int i = 0; i &lt; al.size(); i++)
                     {
                        if ((o.getpredessor()==al.get(i).getpredessor())) //(o.getnodenumber()==al.get(i).getnodenumber()) &amp;&amp;
                        {
                             return i;
                        }
                         else if((o.getpredessor()==al.get(i).getnodenumber())&amp;&amp;(o.getnodenumber()==al.get(i).getpredessor()))
                        {
                            return i;
                        }
                     }
                 }
          return -1;
  }
  </code></pre>
  
  <p>The problem is that the algorithm is visiting all nodes. The other problem is the sorted openlist, which is pushing neighbors of the currentnode up, because they have a lower f value. So what I'm duing wrong by implementing this algorithm?</p>