本文整理汇总了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