Python || Topological sort
\tclass Solution:\n\t\tdef getAncestors(self, n: int, edges: List[List[int]]) -> List[List[int]]:\n\t\t\t\n\t\t\tgraph = defaultdict(list)\n\t\t\tin_degree = [0
\tclass Solution:\n\t\tdef getAncestors(self, n: int, edges: List[List[int]]) -> List[List[int]]:\n\t\t\t\n\t\t\tgraph = defaultdict(list)\n\t\t\tin_degree = [0]*n\n\t\t\tres = [set() for _ in range(n)]\n\t\t\t\n\t\t\t# build the graph and count the in degree for each node\n\t\t\tfor i in edges:\n\t\t\t\tgraph[i[0]].append(i[1])\n\t\t\t\tin_degree[i[1]] += 1\n\t\t\t\n\t\t\tque = deque()\n\t\t\t# nodes with an in degree of 0 are added in the que \n\t\t\tfor i in range(len(in_degree)):\n\t\t\t\tif not in_degree[i]:\n\t\t\t\t\tque.append(i)\n\t\t\t\t\t\n\t\t\t\n\t\t\twhile que:\n\t\t\t\tnode = que.popleft()\n\t\t\t\t\n\t\t\t\tfor i in graph[node]:\n\t\t\t\t\t# since this node is already visited, we can decrease the in degree of it\'s children by 1 \n\t\t\t\t\tin_degree[i] -= 1\n\t\t\t\t\tres[i].add(node)\n\t\t\t\t\t\n\t\t\t\t\t# for the children nodes, we need to add the path of their ancestor \n\t\t\t\t\tfor j in res[node]:\n\t\t\t\t\t\tres[i].add(j)\n\t\t\t\t\tif not in_degree[i]:\n\t\t\t\t\t\tque.append(i)\n\t\t\t\t\t\t\n\t\t\tfor i in range(len(res)):\n\t\t\t\tres[i] = sorted(res[i])\n\t\t\t   \n\t\t\treturn res
['Breadth-First Search', 'Topological Sort', 'Python']