本文整理匯總了Python中PriorityQueue.PriorityQueue.remove_task方法的典型用法代碼示例。如果您正苦於以下問題:Python PriorityQueue.remove_task方法的具體用法?Python PriorityQueue.remove_task怎麽用?Python PriorityQueue.remove_task使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類PriorityQueue.PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.remove_task方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: Prims
# 需要導入模塊: from PriorityQueue import PriorityQueue [as 別名]
# 或者: from PriorityQueue.PriorityQueue import remove_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