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


Python Stack.extend方法代码示例

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


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

示例1: LEXdfs

# 需要导入模块: from stack import Stack [as 别名]
# 或者: from stack.Stack import extend [as 别名]
def LEXdfs(graph, start):
    """
    Does DFS search of graph, beginning at start.
    Implemented from Algorithm 1 in
    "Finding compact communities in large graphs"
    by Creusefond, Largillier and Peyronnet.
    http://dx.doi.org/10.1145/2808797.2808868 
    """

    #
    # Create and initialize VISITED and LEX for all nodes
    #
    attrs = { VISITED: {},
              LEX: {}}
    
    node = graph.BegNI()
    while node < graph.EndNI():
        attrs[VISITED][node.GetId()] = 0
        attrs[LEX][node.GetId()] = "0"
        node.Next()
    
    # initialize DFS variables
    stack = Stack()
    stack.append( start.GetId() )
    i = 1

    # do the search
    while len(stack) > 0:

        # print "stack:"
        # print node_list_to_str(graph, stack, attrs)
        # print
        # print
        
        # process top node
        # print
        # stack.print_all()
        # print
        node_id = stack.pop()
        node = graph.GetNI(node_id)
        attrs[VISITED][node_id] = i
        array = []
        
        # find unvisited neighbors of node
        for in_id in range(node.GetOutDeg()):
            out_id = node.GetOutNId(in_id)
            out_node = graph.GetNI(out_id)
            if attrs[VISITED][out_id] == 0:
                # will raise exception if out_node not there
                try:
                    # print "Trying to remove", node_to_str(graph, out_id, attrs)
                    stack.remove(out_id)
                    # print "Removed", node_to_str(graph, out_id, attrs)
                except ValueError as e:
                    # expected to occur
                    pass

                attrs[LEX][out_id] = str(i) + attrs[LEX][out_id]
                array.append(out_id)

            # end of unvisited neighbor
        # end of neighbors

        # print "Not sure if this is correct.  Needs to randomize order for ties"
        # print "Before"
        # print node_list_to_str(graph, array, attrs)
        array.sort(key = lambda n_id: attrs[LEX][n_id])
        randomize_equal_neighbors(graph, array, attrs)
        # print "After"
        # print node_list_to_str(graph, array, attrs)
        # print
        # print
        stack.extend(array)
        i = i + 1
        # print "stack:"
        # print node_list_to_str(graph, stack, attrs)
        # print
        # print

    # end of stack processing
    
    return attrs
开发者ID:fractal13,项目名称:information-spread-project,代码行数:84,代码来源:LEXdfs.py


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