All pastes #405681 Raw Edit

Miscellany

public java v1 · immutable
·published 2007-03-21 21:43 UTC
rendered paste body
/** * Write a description of class DFS here. *  * @author (your name)  * @version (a version number or a date) */public class DFS{    // instance variables - replace the example below with your own    private static final boolean DEBUG = true;    private int storlek;    protected ListGraph G;    protected Object visitResult;    /**     * Constructor for objects of class DFS     */    public DFS()    {        // initialise instance variables        G = new ListGraph(8);        storlek = 0;            }    /**     * An example of a method - replace this comment with your own     *      * @param  y   a sample parameter for a method     * @return     the sum of x and y      */    public void testsearch()    {        System.out.println(G);    }         /**     * Prints the components of g to stdout in depth-first order.     * Each component is written on a separate line.     */    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++) {            storlek = 0;            if (!visited[v]) {                depthFirstSearch(g, v, visited, action);                System.out.println();                System.out.println("Storleken på komponenten är " + storlek);            }        }    }           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 yet been visited.     * The nodes are visited in depth-first order starting at a.     * The act() method in the VertexAction object is called once for     * 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);        storlek++;        VertexIterator vi = g.adjacentVertices(a);        while (vi.hasNext()) {                          depthFirstSearch(g, vi.next(), visited, action);                                      }      }                    }