Implementing Best-First Search

There is a useful PriorityQueue class in java.util.

This class requires that either

in order to take elements off the queue in a particular order.

The following Comparator object will order a priority queue's Vertex objects (recall that state objects implement the Vertex interface) by removing those with the lowest heuristic values first:

       new Comparator<Vertex>() {
            public int compare(Vertex v1, Vertex v2) {
                int h1 = ((State) v1).getHeuristic(finalState);
                int h2 = ((State) v2).getHeuristic(finalState);
                return h1 - h2;
            }
        }