Reputation: 24754
In networkX, I have a tree as DiGraph().
#!/usr/bin/python
# -*- coding: utf-8 -*-
import networkx as nx
t = nx.DiGraph()
t.add_edge(1,'r')
t.add_edge(2,'r')
t.add_edge(3,'r')
t.add_edge(4,2)
t.add_edge(5,2)
t.add_edge(6,5)
print t.edges()
If a take the node 2 of tree.
how I can get the subtree of 2 ?
I expected this subtree
[(4,2),(5,2),(6,5)]
Upvotes: 4
Views: 5519
Reputation: 363487
If you mean the subtree rooted at node 2
, that's
from networkx.algorithms.traversal.depth_first_search import dfs_tree
subtree_at_2 = dfs_tree(t, 2)
Edit: it seems you've reversed the order of nodes in your edges. In a directed tree, all paths proceed from the root to a leaf, not the other way around. dfs_tree(t.reverse(), 2)
gives you the tree that you want, but do change your code.
Upvotes: 13