本文整理匯總了Python中networkx.dfs_tree方法的典型用法代碼示例。如果您正苦於以下問題:Python networkx.dfs_tree方法的具體用法?Python networkx.dfs_tree怎麽用?Python networkx.dfs_tree使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類networkx
的用法示例。
在下文中一共展示了networkx.dfs_tree方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: dfs_tree
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_tree [as 別名]
def dfs_tree(G, source):
"""Return oriented tree constructed from a depth-first-search from source.
Parameters
----------
G : NetworkX graph
source : node, optional
Specify starting node for depth-first search.
Returns
-------
T : NetworkX DiGraph
An oriented tree
Examples
--------
>>> G = nx.Graph()
>>> G.add_path([0,1,2])
>>> T = nx.dfs_tree(G,0)
>>> print(T.edges())
[(0, 1), (1, 2)]
"""
T = nx.DiGraph()
if source is None:
T.add_nodes_from(G)
else:
T.add_node(source)
T.add_edges_from(dfs_edges(G,source))
return T
示例2: test_dfs_tree
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_tree [as 別名]
def test_dfs_tree(self):
T=nx.dfs_tree(self.G,source=0)
assert_equal(sorted(T.nodes()),sorted(self.G.nodes()))
assert_equal(sorted(T.edges()),[(0, 1), (1, 2), (2, 4), (4, 3)])
示例3: test_dfs_tree_isolates
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_tree [as 別名]
def test_dfs_tree_isolates(self):
G = nx.Graph()
G.add_node(1)
G.add_node(2)
T=nx.dfs_tree(G,source=1)
assert_equal(sorted(T.nodes()),[1])
assert_equal(sorted(T.edges()),[])
T=nx.dfs_tree(G,source=None)
assert_equal(sorted(T.nodes()),[1, 2])
assert_equal(sorted(T.edges()),[])
示例4: dfs_postorder
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_tree [as 別名]
def dfs_postorder(self, root):
G = nx.Graph(self.E)
tree_graph = nx.dfs_tree(G,root)
clique_ordering = list(nx.dfs_postorder_nodes(tree_graph,root))
return clique_ordering
示例5: dfs_tree
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_tree [as 別名]
def dfs_tree(G, source=None, depth_limit=None):
"""Returns oriented tree constructed from a depth-first-search from source.
Parameters
----------
G : NetworkX graph
source : node, optional
Specify starting node for depth-first search.
depth_limit : int, optional (default=len(G))
Specify the maximum search depth.
Returns
-------
T : NetworkX DiGraph
An oriented tree
Examples
--------
>>> G = nx.path_graph(5)
>>> T = nx.dfs_tree(G, source=0, depth_limit=2)
>>> list(T.edges())
[(0, 1), (1, 2)]
>>> T = nx.dfs_tree(G, source=0)
>>> list(T.edges())
[(0, 1), (1, 2), (2, 3), (3, 4)]
"""
T = nx.DiGraph()
if source is None:
T.add_nodes_from(G)
else:
T.add_node(source)
T.add_edges_from(dfs_edges(G, source, depth_limit))
return T
示例6: test_dfs_tree
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_tree [as 別名]
def test_dfs_tree(self):
exp_nodes = sorted(self.G.nodes())
exp_edges = [(0, 1), (1, 2), (2, 4), (4, 3)]
# Search from first node
T = nx.dfs_tree(self.G, source=0)
assert_equal(sorted(T.nodes()), exp_nodes)
assert_equal(sorted(T.edges()), exp_edges)
# Check source=None
T = nx.dfs_tree(self.G, source=None)
assert_equal(sorted(T.nodes()), exp_nodes)
assert_equal(sorted(T.edges()), exp_edges)
# Check source=None is the default
T = nx.dfs_tree(self.G)
assert_equal(sorted(T.nodes()), exp_nodes)
assert_equal(sorted(T.edges()), exp_edges)
示例7: test_dfs_tree_isolates
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_tree [as 別名]
def test_dfs_tree_isolates(self):
G = nx.Graph()
G.add_node(1)
G.add_node(2)
T = nx.dfs_tree(G, source=1)
assert_equal(sorted(T.nodes()), [1])
assert_equal(sorted(T.edges()), [])
T = nx.dfs_tree(G, source=None)
assert_equal(sorted(T.nodes()), [1, 2])
assert_equal(sorted(T.edges()), [])
示例8: test_dls_tree
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_tree [as 別名]
def test_dls_tree(self):
T = nx.dfs_tree(self.G, source=3, depth_limit=1)
assert_equal(sorted(T.edges()), [(3, 2), (3, 4)])
示例9: dfs_tree
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_tree [as 別名]
def dfs_tree(G, source=None, depth_limit=None):
"""Return oriented tree constructed from a depth-first-search from source.
Parameters
----------
G : NetworkX graph
source : node, optional
Specify starting node for depth-first search.
depth_limit : int, optional (default=len(G))
Specify the maximum search depth.
Returns
-------
T : NetworkX DiGraph
An oriented tree
Examples
--------
>>> G = nx.path_graph(5)
>>> T = nx.dfs_tree(G, source=0, depth_limit=2)
>>> list(T.edges())
[(0, 1), (1, 2)]
>>> T = nx.dfs_tree(G, source=0)
>>> list(T.edges())
[(0, 1), (1, 2), (2, 3), (3, 4)]
"""
T = nx.DiGraph()
if source is None:
T.add_nodes_from(G)
else:
T.add_node(source)
T.add_edges_from(dfs_edges(G, source, depth_limit))
return T
示例10: test_dfs_tree
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import dfs_tree [as 別名]
def test_dfs_tree(self):
exp_nodes = sorted(self.G.nodes())
exp_edges = [(0, 1), (1, 2), (2, 4), (4, 3)]
# Search from first node
T=nx.dfs_tree(self.G,source=0)
assert_equal(sorted(T.nodes()), exp_nodes)
assert_equal(sorted(T.edges()), exp_edges)
# Check source=None
T = nx.dfs_tree(self.G, source=None)
assert_equal(sorted(T.nodes()), exp_nodes)
assert_equal(sorted(T.edges()), exp_edges)
# Check source=None is the default
T = nx.dfs_tree(self.G)
assert_equal(sorted(T.nodes()), exp_nodes)
assert_equal(sorted(T.edges()), exp_edges)