Site Search:

maze quiz 12

public class DepthFirstPaths {
    private boolean[] marked;
    private int[] edgeTo;
    private int s;
    public DepthFirstPaths(Graph G, int s) {
        marked = new boolean[G.V()];
        edgeTo = new int[G.V()];
        this.s = s;
        dfs(G, s);
    }
    private void dfs(Graph G, int v) {
        //what is missing here?
        for(int w : G.adj(v)) {
            if(!marked[w]) {
                edgeTo[v] = w;
                dfs(G, w);
            }
        }
    }
    public hasPathTo(int v) {return marked[v];}
    public Iterator<Integer> pathTo(int v) {
        if(!hasPathTo(v)) return null;
        Stack<Integer> path = new Stack<>();
        for(int x = v; x != s; x = edgeTo[x]) {
            path.push(x);
        }
        path.push(s);
        return path;
    }
}