當前位置: 首頁>>代碼示例>>Python>>正文


Python PriorityQueue.dequeue方法代碼示例

本文整理匯總了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
開發者ID:nikrdc,項目名稱:Python-Data-Structures-and-Algorithms,代碼行數:36,代碼來源:Graph.py

示例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 = " ")
開發者ID:EthanSeaver,項目名稱:Python-Projects,代碼行數:17,代碼來源:TestPriorityQueue.py


注:本文中的PriorityQueue.PriorityQueue.dequeue方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。