本文整理汇总了Python中PriorityQueue.PriorityQueue.dequeue方法的典型用法代码示例。如果您正苦于以下问题:Python PriorityQueue.dequeue方法的具体用法?Python PriorityQueue.dequeue怎么用?Python PriorityQueue.dequeue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue.PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.dequeue方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: shortest_path
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import dequeue [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 dequeue [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 = " ")