Does anyone know if python networkx pre-computes node depths for a tree created using a Networkx DiGraph?
Since I have not seen a way to define trees in networkx, maybe it would be necessary to just compute the node depths using an external data structure.
For NetworkX, a graph with more than 100K nodes may be too large. I'll demonstrate that it can handle a network with 187K nodes in this post, but the centrality calculations were prolonged. Luckily, there are some other packages available to help us with even larger graphs.
Using networkx we can load and store complex networks. We can generate many types of random and classic networks, analyze network structure, build network models, design new network algorithms and draw networks.
Returns True if the graph has an edge between nodes u and v. This is the same as v in G[u] or key in G[u][v] without KeyError exceptions.
If you know the root of the tree you can use the shortest_path() function to get the distance from the root:
In [1]: import networkx as nx
In [2]: G = nx.DiGraph()
In [3]: G.add_path([0,10,20,30])
In [4]: G.add_path([0,1,2,3])
In [5]: nx.shortest_path_length(G,0) # assume a tree rooted at node 0
Out[5]: {0: 0, 1: 1, 2: 2, 3: 3, 10: 1, 20: 2, 30: 3}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With