797. All Paths From Source to Target

Given a directed, acyclic graph of N nodes.  Find all possible paths from node 0 to node N-1, and return them in any order.
The graph is given as follows:  the nodes are 0, 1, ..., graph.length - 1.  graph[i] is a list of all nodes j for which the edge (i, j) exists.
Example:
Input: [[1,2], [3], [3], []] 
Output: [[0,1,3],[0,2,3]] 
Explanation: The graph looks like this:
0--->1
|    |
v    v
2--->3
There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.
Note:
* The number of nodes in the graph will be in the range [2, 15].
* You can print different paths in any order, but you should keep the order of nodes inside one path.









class Solution {
    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        int n = graph.length - 1;
        List<Integer> tmp = new ArrayList<>();
        tmp.add(0);
        List<List<Integer>> res = new ArrayList<>();
        dfs(res, tmp, graph, 0, n);
        
        return res;
    }
    private void dfs(List<List<Integer>> res, List<Integer> tmp, int[][] graph, int start, int end){
        // base case 
        if(start == end){
            res.add(new ArrayList<>(tmp));
            return;
        }
        
        for(int nei : graph[start]){
            tmp.add(nei);
            dfs(res, tmp, graph, nei, end);
            tmp.remove(tmp.size() - 1);
        }
    }
}


Dfs + memo 


带返回值的dfs recursion, https:
//leetcode.com/problems/all-paths-from-source-to-target/discuss/118713/Java-DFS-Solution
Another dfs solution is to use memorization. Each node will be only visited once since the sub result from this node has already been recorded. Memorization increses space cost as well as time cost to record existing paths.

class Solution {
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
    Map<Integer, List<List<Integer>>> map = new HashMap<>();

    return dfsSearch(graph, 0, map);   
}

private List<List<Integer>> dfsSearch(int[][] graph, int node, Map<Integer, List<List<Integer>>> map) {
    if (map.containsKey(node)) {
        return map.get(node);
    }

    List<List<Integer>> res = new ArrayList<>();
    if (node == graph.length - 1) {
        List<Integer> path = new LinkedList<>();
        path.add(node);
        res.add(path);
    } else {
        for (int nextNode : graph[node]) {
            List<List<Integer>> subPaths = dfsSearch(graph, nextNode, map);
            for (List<Integer> path : subPaths) {
                LinkedList<Integer> newPath = new LinkedList<>(path);
                newPath.addFirst(node);
                res.add(newPath);
            }
        }
    }

    map.put(node, res);
    return res;
}
}

 

 

 

posted on 2018-11-06 09:28  猪猪&#128055;  阅读(165)  评论(0)    收藏  举报

导航