本文整理匯總了Python中priorityQueue.PriorityQueue.buildHeap方法的典型用法代碼示例。如果您正苦於以下問題:Python PriorityQueue.buildHeap方法的具體用法?Python PriorityQueue.buildHeap怎麽用?Python PriorityQueue.buildHeap使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類priorityQueue.PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.buildHeap方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: prim
# 需要導入模塊: from priorityQueue import PriorityQueue [as 別名]
# 或者: from priorityQueue.PriorityQueue import buildHeap [as 別名]
def prim(aGraph, startVertex):
# create a priority queue that uses distance as the value to determine priority and thus its position
# use the distance to the vertex as the priority because while exploring the next vertex, want to explore the vertex that has the smallest distance
# decreaseKey method used when the distance to a vertex that is already in the queue is reduced, and thus moves that vertex toward the front of the queue.
pQueue = PriorityQueue()
# initialize the state of the graph
for vertex in aGraph:
# initialally all vertices values are = infinity (sys.maxint) because we assume the greatest value and then update appropiately
vertex.setDistance(sys.maxsize)
vertex.setPred(None)
# distance represents distance from the startVertex, trivially 0 for the startVertex
startVertex.setDistance(0)
# key value pair
# key is distance and vertex is the value
pQueue.buildHeap([ (vertex.getDistance(), vertex) for vertex in aGraph ])
while not pQueue.isEmpty():
currentVertex = pQueue.delMin()
# iterate over the currentVertex's edges
for nextVertex in currentVertex.getConnections():
# calculate the weight from currentVertex to nextVertex
newCost = currentVertex.getWeight(nextVertex)
# found a shorter path
# node is not considered to be part of the spanning tree until it is removed from the priority queue.
# nextVertex in pQueue means that vertex is not yet in the spanning tree so it is safe to add (ensures an acyclic graph)
if nextVertex in pQueue and newCost< nextVertex.getDistance():
# assign the predecessor appropiately
nextVertex.setPred(currentVertex)
# set a new distance on the nextVertex
nextVertex.setDistance(newCost)
# update the priorityQueue with the correct values
pQueue.decreaseKey(nextVertex, newCost)
示例2: dijkstra
# 需要導入模塊: from priorityQueue import PriorityQueue [as 別名]
# 或者: from priorityQueue.PriorityQueue import buildHeap [as 別名]
def dijkstra(aGraph, startVertex):
# create a priority queue that uses distance as the value to determine priority and thus its position
# use the distance to the vertex as the priority because while exploring the next vertex, want to explore the vertex that has the smallest distance
# decreaseKey method used when the distance to a vertex that is already in the queue is reduced, and thus moves that vertex toward the front of the queue.
pQueue = PriorityQueue()
# distance represents distance from the startVertex, trivially 0 for the startVertex
# initialally all vertices values are = infinity (sys.maxint) because we assume the greatest value and then update appropiately
startVertex.setDistance(0)
# key value pair
# key is distance and vertex is the value
pQueue.buildHeap([ (vertex.getDistance(), vertex) for vertex in aGraph ])
while not pQueue.isEmpty():
currentVertex = pQueue.delMin()
# iterate over the currentVertex's edges
for nextVertex in currentVertex.getConnections():
# distance of current vertex and the weight of it's edges
newDistance = currentVertex.getDistance() + currentVertex.getWeight(nextVertex)
# found a shorter path
if newDistance < nextVertex.getDistance():
# set a new distance on the nextVertex
nextVertex.setDistance(newDistance)
# assign the predecessor appropiately
nextVertex.setPred(currentVertex)
# update the priorityQueue with the correct values
pQueue.decreaseKey(nextVertex, newDistance)
示例3: Dijkstras
# 需要導入模塊: from priorityQueue import PriorityQueue [as 別名]
# 或者: from priorityQueue.PriorityQueue import buildHeap [as 別名]
def Dijkstras(graph,start):
pq=PriorityQueue()
start.setDistance(0)
pq.buildHeap([(v.getDistance(),v) for v in graph]) # distance is the key in the priority queue
while not pq.isEmpty():
currentvertex=pq.delMin()
for newvertex in currentvertex.getConnections():
newDist=currentvertex.getDistance()+currentvertex.getWeight(newvertex)
if newDist<newvertex.getDistance():
# at the start the distance of all the vertices is set to maximum. That's why the if statement will be executed
newvertex.setDistance(newDist)
newvertex.setPredecessor(currentvertex)
pq.decreaseKey(newvertex,newDist)
示例4: prim
# 需要導入模塊: from priorityQueue import PriorityQueue [as 別名]
# 或者: from priorityQueue.PriorityQueue import buildHeap [as 別名]
def prim(graph,start): # it belongs to the family of greedy algorithms
pq=PriorityQueue()
for v in graph:
v.setDistance(sys.maxsize)
v.setPredecessor(None)
start.setDistance(0)
pq.buildHeap([(v.getDistance(),v) for v in graph])
while not pq.isEmpty()
currentvertex=pq.delMin()
for newvertex in currentvertex.getConnections():
newDist=currentvertex.getWeight(newvertex)
if newDist<newvertex.getDistance() and newvertex in pq:
newvertex.setDistance(newDist)
newvertex.setPredecessor(currentvertex)
pq.decreaseKey(newvertex,newDist)