All pastes #414151 Raw Edit

Stuff

public java v1 · immutable
#414151 ·published 2007-03-28 21:25 UTC
rendered paste body
    public LinkedList<Integer> BFS(UndirectedGraph g, int start, int goal) {     	Queue<LinkedList<Integer>> routes = new ArrayDeque<LinkedList<Integer>>(g.numVertices());    	LinkedList<Integer> vertices = new LinkedList<Integer>();    	vertices.add(new Integer(start));    	while (!routes.isEmpty()) {    		LinkedList<Integer> currentRoute = routes.poll();    		int currentVertex = currentRoute.poll();    		VertexIterator vi = g.adjacentVertices(currentVertex);    		if(vi.hasNext()) {    			while(vi.hasNext()) {    				int adjacentVertex = vi.next();    				LinkedList<Integer> newRoute;    				newRoute = currentRoute;    				newRoute.add(adjacentVertex);    				routes.add(newRoute);    				if(adjacentVertex == goal) {    					return newRoute;    				}    			}    		} else {    			return currentRoute;    		}    	}		return new LinkedList<Integer>();	    }