A General Graph Search Method

Note how the search method uses the Strategy design pattern to decouple the search from the double-ended queue adding strategy:

    /**
     * The core search operation for this graph.
     * It uses a double-ended queue so that either breadth-first or
     * depth-first search can be performed depending on to which end of
     * the queue newly discovered vertices are added.
     * @param start The start vertex for the search
     * @param adder A purely functional object that adds to either the 
     * head or tail of a double-ended queue
     */
    private void search(Vertex start, DequeAdder adder) {
        searchInit();
        start.setOpen(false);
        start.setDistance(0);
        Deque<Vertex> deque = new LinkedList<Vertex>();
        deque.add(start);
        while ( !deque.isEmpty() ) {
            Vertex v = deque.remove();
            List<Vertex> adjList = adj_hash.get(v);
            if ( adjList != null ) {
                Iterator iter = adjList.iterator();
                while ( iter.hasNext() ) {
                    Vertex successor = (Vertex) iter.next();
                    if ( successor.isOpen() ) {
                        successor.setOpen(false);
                        successor.setDistance(v.getDistance()+1);
                        successor.setPredecessor(v);
                        adder.add(successor, deque);
                    }
                }
            }
        }
    }

    /**
     * An initializing operation for the core search operation.
     * All vertices are set to open with null predecessors and
     * a distance of infinity from the start.
     */
    private void searchInit() {
        Iterator<Vertex> iterator = vertices.iterator();
        while ( iterator.hasNext() ) {
            Vertex vertex = (Vertex) iterator.next();
            vertex.setOpen(true);
            vertex.setDistance(INFINITY);
            vertex.setPredecessor(null);
        }
    }