当前位置: 首页>>代码示例>>Python>>正文


Python DiGraph.add_path方法代码示例

本文整理汇总了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
开发者ID:davidhildebrand,项目名称:CATMAID,代码行数:41,代码来源:tree_util.py


注:本文中的networkx.DiGraph.add_path方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。