本文整理匯總了Python中PriorityQueue.PriorityQueue.enqueue方法的典型用法代碼示例。如果您正苦於以下問題:Python PriorityQueue.enqueue方法的具體用法?Python PriorityQueue.enqueue怎麽用?Python PriorityQueue.enqueue使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類PriorityQueue.PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.enqueue方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: shortest_path
# 需要導入模塊: from PriorityQueue import PriorityQueue [as 別名]
# 或者: from PriorityQueue.PriorityQueue import enqueue [as 別名]
def shortest_path(self, target):
"""
Uses Dijkstra's algorithm to return the shortest paths between the
target and all other nodes in the graph.
"""
if not self.weighted:
print "Graph is not weighted; redirecting to BFS"
return self.bfs(target, min_distance=True)
distances_pq = PriorityQueue("min")
distances_dict = {}
unvisited = []
for node in self.adj_matrix[0]:
if node == target:
distances_pq.enqueue(0, node)
distances_dict[node] = 0
elif node != False:
distances_pq.enqueue(float('inf'), node)
distances_dict[node] = float('inf')
else:
continue
unvisited.append(node)
while unvisited:
min_distance, min_node = distances_pq.dequeue()
if min_node not in unvisited:
continue
neighbors = self.linked(min_node)
for neighbor in neighbors:
neighbor_distance = min(distances_dict[neighbor],
min_distance + self.link_weight(min_node, neighbor))
if neighbor_distance != distances_dict[neighbor]:
distances_dict[neighbor] = neighbor_distance
distances_pq.enqueue(neighbor_distance, neighbor)
unvisited.remove(min_node)
return distances_dict
示例2: PriorityQueue
# 需要導入模塊: from PriorityQueue import PriorityQueue [as 別名]
# 或者: from PriorityQueue.PriorityQueue import enqueue [as 別名]
from PriorityQueue import PriorityQueue
patient1 = [2, "John"]
patient2 = [1, "Jim"]
patient3 = [5, "Tim"]
patient4 = [7, "Cindy"]
priorityQueue = PriorityQueue()
priorityQueue.enqueue(patient1)
priorityQueue.enqueue(patient2)
priorityQueue.enqueue(patient3)
priorityQueue.enqueue(patient4)
while priorityQueue.getSize() > 0:
print(str(priorityQueue.dequeue()), end = " ")