DEV Community

Prashant Mishra
Prashant Mishra

Posted on • Originally published at leetcode.com

2 1

("Graph") 797. All Paths From Source to Target

Given a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all possible paths from node 0 to node n - 1 and return them in any order.

The graph is given as follows: graph[i] is a list of all nodes you can visit from node i (i.e., there is a directed edge from node i to node graph[i][j]).

Example:
graph

Input: graph = [[1,2],[3],[3],[]]
Output: [[0,1,3],[0,2,3]]
Explanation: There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.
Enter fullscreen mode Exit fullscreen mode
class Solution {
    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        // we have to find all the possible path from source to destination.
        // we can do that with depth first search.
        //we can keep track of visited nodes in a list in a depth first search manner.
        //if we reach the target node we will store list 'l', and we will backtrack to adjacent nodes of the previous node to check all the possible paths
        List<List<Integer>> list = new ArrayList<>();
        dfs(0,graph,new ArrayList<>(Arrays.asList(0)),list); //initially we are also adding 0 in the list l, as the starting node
        return list;
    }
    public void dfs(int node, int[][] graph,List<Integer> l, List<List<Integer>> list){
        if(node ==graph.length-1){
            list.add(new ArrayList<>(l));
            return;
        }
        // below loop will give array of all adjacent nodes of current node i.e 'node'
        for(int i : graph[node]){
            //take
            l.add(i);
            dfs(i,graph,l,list);
            //don't take
            l.remove(l.size()-1);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.