sunny
sunny

Reputation: 355

Can not understand the resulte of the dfs_tree in networkx?

I've created a graph and I want to verify all the sub-trees( the purpose is visualize the hierarchy) but there is an error and also the results for each node are the same while I know for example node1 has a sub-tree and node 10 has not a sub tree :

>      G = nx.from_pandas_dataframe(db, 'name1','name2','weight',nx.Graph()) 
`enter code here`nodes       = G.nodes() edges= G.edges()
> node_and_degree=G.degree()
> 
> d=dict(zip(G.nodes(),G.edges()))
> 
> degree=list(zip(G.nodes(),G.degree().values())) degree_sort=
> sorted(degree,key=itemgetter(1), reverse=True)  


inner_nodes1 = [ n for n in nodes if G.degree(n) >=200 ] 
inner_nodes2 = [ n for n in nodes if (100<= G.degree(n)  <200) ] 
inner_nodes3 = [ n for n in nodes if (50<= G.degree(n)  <100) ] 
inner_nodes4 = [ n for n in nodes if (20<= G.degree(n)  <50) ]
inner_nodes5 = [ n for n in nodes if (10<= G.degree(n)  <20) ] 
inner_nodes6=  [ n for n in nodes if (2<= G.degree(n)  <10) ]  
leaves      = set( n for n in nodes if  G.degree(n) == 1 )
inner_nodes = [ n for n in nodes if G.degree(n) >1 ]

> from collections import defaultdict from
> networkx.algorithms.traversal.depth_first_search import dfs_tree
> 
> for i in range(len(inner_nodes)):
> 
>     print( dfs_tree(G,i))
> 
> --------------------------------------------------------------------------- KeyError                                  Traceback (most recent call
> last) <ipython-input-25-4494433c2afc> in <module>()
>       4 for i in range(len(inner_nodes)):
>       5 
> ----> 6     print( dfs_tree(G,i))
>       7 
> 
> C:\ProgramData\Anaconda3\lib\site-packages\networkx\algorithms\traversal\depth_first_search.py
> in dfs_tree(G, source)
>      99     else:
>     100         T.add_node(source)
> --> 101     T.add_edges_from(dfs_edges(G,source))
>     102     return T
>     103 
> 
> C:\ProgramData\Anaconda3\lib\site-packages\networkx\classes\digraph.py
> in add_edges_from(self, ebunch, attr_dict, **attr)
>     633                     "The attr_dict argument must be a dict.")
>     634         # process ebunch
> --> 635         for e in ebunch:
>     636             ne = len(e)
>     637             if ne==3:
> 
> C:\ProgramData\Anaconda3\lib\site-packages\networkx\algorithms\traversal\depth_first_search.py
> in dfs_edges(G, source)
>      59             continue
>      60         visited.add(start)
> ---> 61         stack = [(start,iter(G[start]))]
>      62         while stack:
>      63             parent,children = stack[-1]
> 
> C:\ProgramData\Anaconda3\lib\site-packages\networkx\classes\graph.py
> in __getitem__(self, n)
>     405         {1: {}}
>     406         """
> --> 407         return self.adj[n]
>     408 
>     409     def add_node(self, n, attr_dict=None, **attr):
> 
> KeyError: 0
> 
> len(list(dfs_tree(G,'1')))
 1000 

len(list(dfs_tree(G,'10')))

 1000

I couldn't understand results , may be I'm in the wrong path , any help or comment appreciated !

Upvotes: 0

Views: 811

Answers (1)

Joel
Joel

Reputation: 23887

for i in range(len(inner_nodes)) causes the first i to be 0. The error message says that G does not have a node 0.

As for 10 and 1 having the same size trees:

If your graph is an undirected graph, then the depth first search will find the entire component containing the node. It doesn't know you want it to find a subtree, because that concept doesn't exist in an undirected graph. So if 1 and 10 are in the same tree, it'll give the same set of nodes (but with different orders).

Upvotes: 1

Related Questions