

public class LongPath {
    public static void main(String args[]) {
        int n =0;
        if (args.length < 1) {
            System.err.println("Usage: java LongPath <n> [--nonrec]");
            System.err.println("Creates a path on n vertices and runs a recursive implementation of DFS to see whether it crashes");
            System.err.println("If option --nonrec is specified, it runs a non-recursive version of DFS instead.");
            System.exit(-1);
        }
        boolean recursive = true; 
        if (args.length >= 2) {
            if (args[1].equals("--nonrec")) {
                recursive = false; 
            }
            else {
                System.err.println("unknown option: " + args[1]);
                System.exit(-1);
            }
        }
        try {
            n = Integer.parseInt(args[0]);            
        } catch (NumberFormatException e) {
            System.err.println("Usage java LongPath <n>");
            System.err.println("n must be integer");
            System.exit(-1);
        }
        System.err.println("I will generate a path of length " + (n-1) + " and then run DFS to see whether the program crashes.");
        Graph graph = new Graph (n);
        for (int i = 1; i < n; i++) {
            graph.addEdge(i-1, i);
        }
        int s = 0;
        boolean[] visited = new boolean[n];
        if (recursive) {
            UndirectedConnectivity.depthFirstSearch(s, visited, graph);
        }
        else {
            UndirectedConnectivity.dfsNonrecursive(s, visited, graph);
        }
        
        System.err.println("reachable from " + s + ": ");
        for (int u = 0; u < n; u++) {
          System.out.println(u + ": " + visited[u]);
        }
    }
}