All pastes #405464 Raw Edit

Stuff

public java v1 · immutable
·published 2007-03-21 19:31 UTC
rendered paste body
public class DFS{	// instance variables - replace the example below with your own	private static final boolean DEBUG = true;	protected ListGraph G;	protected Object visitResult;	/**	 * Constructor for objects of class DFS	 */	public DFS()	{		// initialise instance variables		G = new ListGraph(8);	}	/**	 * 	 */	public void testprint()	{		System.out.println(G);	}		 /**     * Prints the components of g      *      */    protected void printComponentsDFS(ListGraph g) {        int size = g.numVertices();        boolean[] visited = new boolean[size];        VertexAction action = new PrintAction();                System.out.println();        System.out.println("The components of g (DFS):");                    // Do a DFS for each node that has not yet been visited.        for (int v = 0; v < size; v++) {            if (!visited[v]) {                depthFirstSearch(g, v, visited, action);                System.out.println();            }        }    }    		private static class PrintAction implements VertexAction {	            public void act(UndirectedGraph g, int v) {            System.out.print(v + " ");        }    }           /**     * Traverses the nodes of g that have not been visited.     * vertexaction "paints" each node     *      * @param  g an undirected graph.     * @param  a start vertex.     * @param  visited visited[i] is true iff node i has been visited.     */    protected void depthFirstSearch(UndirectedGraph g, int a, boolean[] visited, VertexAction action) {         if (visited[a])            return;                    visited[a] = true;        action.act(g, a);                VertexIterator vi = g.adjacentVertices(a);        while (vi.hasNext()) {             depthFirstSearch(g, vi.next(), visited, action);            }      }