Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does networkx keep track of node depths?

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.

like image 663
HeyWatchThis Avatar asked Nov 08 '13 00:11

HeyWatchThis


People also ask

How many nodes can NetworkX handle?

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.

What can I do with NetworkX?

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.

Does NetworkX have edge?

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.


1 Answers

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}
like image 111
Aric Avatar answered Oct 06 '22 12:10

Aric