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


Python Graph.nbunch_iter方法代码示例

本文整理汇总了Python中networkx.Graph.nbunch_iter方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.nbunch_iter方法的具体用法?Python Graph.nbunch_iter怎么用?Python Graph.nbunch_iter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在networkx.Graph的用法示例。


在下文中一共展示了Graph.nbunch_iter方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from networkx import Graph [as 别名]
# 或者: from networkx.Graph import nbunch_iter [as 别名]

#.........这里部分代码省略.........
        """
        recursive helper function for get_all_paths_from
        :param in_edges: incoming edges, aka dominoes already in the chain
        :param new_edge: the edge to add, aka the latest domino
        :param in_visited_edges: set of keys for the dominoes already in the chain
        :param new_key: the key for the edge to be added
        :param kwds: used by NetworkX for things
        :param out_edges: All remaining dominoes
        :param key: the key function
        :return: a Path list where each path starts with in_edges and continues to all possible outcomes
        """
        edges = copy(in_edges)
        visited_edges = copy(in_visited_edges)
        edges.append(new_edge)
        visited_edges.add(new_key)
        node = new_edge[1]

        paths = []
        for edge in out_edges(node, **kwds):
            edge_key = key(edge)
            if edge_key not in visited_edges:
                paths.extend(self._get_more_edges(edges, edge, visited_edges, edge_key, kwds, out_edges, key))
        paths.append(Path(edges))
        return paths

    def get_all_paths_from(self, origin):
        """
        Finds all paths through the dominoes graph starting at the specified origin number.
        The returned paths will visit each edge (aka domino) no more than once.
        :param origin: The number to start on, or an iterable containing start numbers.
            All paths will start with that number.
        :return: A Path list for all possible paths.
        """
        nodes = list(self.graph.nbunch_iter(origin))
        if not nodes:
            raise StopIteration

        kwds = {'data': False}
        out_edges, key, tailhead = helper_funcs(self.graph, 'original')

        visited_edges = set()
        paths = {}

        for node in set(nodes):
            paths[node] = []
            for edge in out_edges(node, **kwds):
                edge_key = key(edge)
                if edge_key not in visited_edges:
                    paths[node].extend(self._get_more_edges([], edge, set(), edge_key, kwds, out_edges, key))
        return paths

    def get_longest_paths_from(self, origin):
        """
        Find all paths through the domino graph starting at the specified origin number which have the greatest length.
        The returned paths will visit each edge (aka domino) no more than once.
        :param origin: The number to start on, or an iterable containing start numbers.
            All paths will start with one of those number.
        :return: A Path list for all possible paths of greatest length.
        """
        long_paths = [Path([])]
        for paths in self.get_all_paths_from(origin).values():
            for path in paths:
                if path.size == long_paths[0].size:
                    long_paths.append(path)
                elif path.size > long_paths[0].size:
                    long_paths = [path]
开发者ID:Pellanor,项目名称:MexicanTrain,代码行数:70,代码来源:BotGameState.py


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