本文整理汇总了Python中networkx.DiGraph.add_path方法的典型用法代码示例。如果您正苦于以下问题:Python DiGraph.add_path方法的具体用法?Python DiGraph.add_path怎么用?Python DiGraph.add_path使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx.DiGraph
的用法示例。
在下文中一共展示了DiGraph.add_path方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: spanning_tree
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import add_path [as 别名]
def spanning_tree(tree, preserve):
""" Return a new DiGraph with the spanning tree including the desired nodes.
preserve: the set of nodes that delimit the spanning tree. """
spanning = DiGraph()
preserve = set(preserve) # duplicate, will be altered
if 1 == len(preserve):
spanning.add_node(iter(preserve).next())
return spanning
if len(tree.successors(find_root(tree))) > 1:
tree = tree.copy()
# First end node found
endNode = (node for node in tree if not next(tree.successors_iter(node), None)).next()
reroot(tree, endNode)
n_seen = 0
# Start from shortest sequence
for seq in sorted(partition(tree), key=len):
path = []
for node in seq:
if node in preserve:
path.append(node)
if node not in spanning:
n_seen += 1
if len(preserve) == n_seen:
break
elif path:
path.append(node)
if path:
spanning.add_path(path)
if seq[-1] == path[-1]:
preserve.add(path[-1])
if len(preserve) == n_seen:
break
return spanning