Assuming I have this code which creates a DiGraph:
dict = {1:['a1', 'a2', 'a3'], 2:['a4', 'a5','a7']}
graph = nx.from_dict_of_lists(dict)
digraph = nx.DiGraph(graph)
How can I find all the roots in this graph? expected output for this graph is [1,2]
If it's is somewhat more convienent to you, I have written the code inside a google colab notebook where you can see the graph, hope it helps.
EDIT: this is somehow related to this question the difference is that in that post there was an assumption of that the graph is connected so there is only one root; it's not the case in my example. Can I 'divide' my graph to connected sub-graphs and then search for a root in each one?