本文整理汇总了Python中PriorityQueue.PriorityQueue.pop_task方法的典型用法代码示例。如果您正苦于以下问题:Python PriorityQueue.pop_task方法的具体用法?Python PriorityQueue.pop_task怎么用?Python PriorityQueue.pop_task使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue.PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.pop_task方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Prim
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import pop_task [as 别名]
def Prim(G, w, s):
inf = float("inf")
H = PriorityQueue()
T = []
s.priorite = 0
H.add_task(s, 0)
i = 1
for u in G.sommets:
u.pred = None
if u != s :
H.add_task(u, inf)
u.priorite = inf
try:
while True:
u = H.pop_task()
u.couleur = "noir"
if u.priorite != inf:
if u.pred is not None: T.append((u.pred, u))
for v in u.voisins:
if v.couleur == "blanc": #v n'est pas sorti du tas
if w[(u, v)] < v.priorite:
v.priorite = w[(u, v)]
H.add_task(v, v.priorite)
v.pred = u
except:
pass
return T
示例2: Prims
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import pop_task [as 别名]
def Prims( graph, start ):
''' Creates a minimum spanning tree using Prims algorithm
'''
print "Start vertex = ", start
spanningTree = Graphs.Graph( graph.maxVertices, False )
inTree = [False]*graph.maxVertices
inTree[start] = True
spanningVertices = [] #[0]*graph.maxVertices
spanningVertices.append(start)
# Initialize the heap (PriorityQueue) with the min edge of the first
# vertex for its key, and ininity for all other keys (vertexs).
heapQueue = PriorityQueue()
#for vertex in range(0, len(graph.vertices)):
# if( not (graph.vertices[vertex]).empty ):
# heapQueue.add_task(vertex, 35850023) #sys.maxint)
startEdges = (graph.vertices[start]).priorityEdges
nextVertex, minWeight, noMoreValidEdges = startEdges.pop_task()
heapQueue.add_task( (start, nextVertex), minWeight)
totalCost = 0
while( not noMoreValidEdges ):
# Pop the next cheapest edge off the list off the heap (prioirty queue) (this will remove it)
nextEdge, cheapestWeight, noMoreValidEdges = heapQueue.pop_task()
if( not noMoreValidEdges ):
nextVertex = nextEdge[1]
vertexInTreeGettingUpdate = nextEdge[0]
if( not inTree[nextVertex] ):
#print "Next vertex, weight, total = ", nextVertex, cheapestWeight, totalCost
totalCost += cheapestWeight
inTree[nextVertex] = True
spanningVertices.append(nextVertex)
spanningTree.InsertEdge(vertexInTreeGettingUpdate, nextVertex, False, cheapestWeight)
# Need to find the cheapest edges for both vertex's on the next edge that is
# coming into the spanning tree.
# Note that the edge that led to this vertex being selected has now been
# removed from the "priorityEdges" for that vertex by the "pop_task()" function below
nextEdges = (graph.vertices[nextVertex]).priorityEdges
edgesEmpty = False
cheapestEdgeVertex = nextVertex
# We keep popping the edges until we find one that's not already in the tree.
while( (not edgesEmpty) and (inTree[cheapestEdgeVertex])):
cheapestEdgeVertex, cheapestEdgeWeight, edgesEmpty = nextEdges.pop_task()
#if( (not edgesEmpty) and (inTree[cheapestEdgeVertex])):
# print "edge discarded because already in tree = ", cheapestEdgeVertex, cheapestEdgeWeight
if( not edgesEmpty ):
heapQueue.remove_task( (nextVertex, cheapestEdgeVertex) )
heapQueue.add_task((nextVertex, cheapestEdgeVertex), cheapestEdgeWeight)
nextEdges = (graph.vertices[vertexInTreeGettingUpdate]).priorityEdges
edgesEmpty = False
cheapestEdgeVertex = vertexInTreeGettingUpdate
# We keep popping the edges until we find one that's not already in the tree.
while( (not edgesEmpty) and (inTree[cheapestEdgeVertex])):
cheapestEdgeVertex, cheapestEdgeWeight, edgesEmpty = nextEdges.pop_task()
#if( (not edgesEmpty) and (inTree[cheapestEdgeVertex])):
# print "edge discarded because already in tree = ", cheapestEdgeVertex, cheapestEdgeWeight
if( not edgesEmpty ):
heapQueue.remove_task( (vertexInTreeGettingUpdate, cheapestEdgeVertex) )
heapQueue.add_task( (vertexInTreeGettingUpdate, cheapestEdgeVertex), cheapestEdgeWeight)
return totalCost