Getting the root (head) of a DiGraph in networkx (Python)
If by having “one root element” you mean your directed graph is a rooted tree, then the root will be the only node with zero in-degree. You can find that node in linear time (in the number of nodes) with: In [1]: import networkx as nx In [2]: G=nx.balanced_tree(2,3,create_using=nx.DiGraph()) # tree rooted at 0 In … Read more