Bfs java solution with explanation.
Explanation\n1. Using bfs, We start with the nodes that have an indegree of zero.\n2. Once we process all the nodes with in degree of zero at the current level,
**Explanation**\n1. Using bfs, We start with the nodes that have an indegree of zero.\n2. Once we process all the nodes with in degree of zero at the current level, we move to the next level.\n3. We repeat the above process untill all the nodes are processed.\n\n**Algorithm**\n1. Create an indegree counter array and adjacency map.\n2. Add each node with indegree count of zero the the queue.\n3. Pop the parent node that needs to be processed.\n4. For the current parent, get all the children.\n5. For each child, add all the ancestors of parent to the child list.\n6. Add the current parent to the child list.\n7. Decrement the indegree count of the child node since we processed the current *parent -> child* edge.\n8. If the in degree count of child becomes zero, then no parents are left. we add it to the queue.\n\n**Example**\nInput: [Image](https://assets.leetcode.com/users/images/ca8a3cdd-061d-4dd3-af5a-1884655fe7c3_1646510728.9197474.png)\nMatrix representation: {0:[3,1],1:[3],2:[3,5,1],3:[],4:[3,5,2,0],5:[0,3,1]}\nOutput: [[2,4,5],[0,2,4,5],[4],[0,1,2,4,5],[],[2,4]]\n\n```java\nclass Solution {\n    public List<List<Integer>> getAncestors(int n, int[][] edges) {\n        List<Set<Integer>> list = new ArrayList<>();\n        Map<Integer, List<Integer>> map = new HashMap<>();\n        int[] in = new int[n];\n        \n        // Initializing the path list & adjacency map.\n        for(int i = 0; i < n; i++){\n            list.add(new HashSet<>());\n            map.put(i, new ArrayList<>());\n        }\n        \n        // Processing all the edges. \n        // For each edge add the child to adjacency matrix and incrment it\'s indegree count.\n        for(int[] edge: edges){\n            int u = edge[0], v = edge[1];\n            map.get(u).add(v);\n            in[v]++;\n        }\n        \n        // Intializing queue & add adding all nodes to with indegree count as zero to queue.\n        Queue<Integer> q = new LinkedList<>();\n        for(int i = 0; i < n; i++){\n            if(in[i] == 0){\n                q.add(i);\n            }\n        }\n        \n        // perform bfs traversal.\n        // we add new node to the queue when its in degree becomes zero.\n        while(!q.isEmpty()){\n            int node = q.poll();\n            for(int child: map.get(node)){\n                list.get(child).addAll(list.get(node));\n                list.get(child).add(node);\n                in[child]--;\n                if(in[child] == 0){\n                    q.add(child);\n                }\n            }\n        }\n        \n        // Convert the list into required format.\n        List<List<Integer>> result = new ArrayList<>();\n        for(Set<Integer> s : list){\n            result.add(new ArrayList<>(s));\n            Collections.sort(result.get(result.size()-1));\n        }\n        return result;\n    }\n}\n```\n\nplease upvote if this is simple & easy to understand/implement. Thanks :)
['Breadth-First Search', 'Java']