本文整理汇总了Python中priorityQueue.PriorityQueue.insert方法的典型用法代码示例。如果您正苦于以下问题:Python PriorityQueue.insert方法的具体用法?Python PriorityQueue.insert怎么用?Python PriorityQueue.insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类priorityQueue.PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.insert方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dijkstra
# 需要导入模块: from priorityQueue import PriorityQueue [as 别名]
# 或者: from priorityQueue.PriorityQueue import insert [as 别名]
def dijkstra(graphObj, startVertex):
pq = PriorityQueue()
for vertex in graphObj.getVertices():
if vertex == startVertex:
pq.insert([0, vertex])
graphObj.verticesList[startVertex].distance = 0
else:
pq.insert([INFINITY, vertex])
while(len(pq.pqueue)):
currentVertex = pq.extractMin()
if len(pq.pqueue) == 1:
break
# print(pq.pqueue, pq.lookup)
for adjNode in graphObj.verticesList[currentVertex[1]].getConnections():
newDistance = graphObj.verticesList[currentVertex[1]].distance + graphObj.verticesList[currentVertex[1]].adjList[adjNode]
if newDistance < graphObj.verticesList[adjNode].distance:
graphObj.verticesList[adjNode].distance = newDistance
graphObj.verticesList[adjNode].predecessor = currentVertex[1]
index = pq.lookup[adjNode]
pq.decreaseKey(index, newDistance)
return graphObj
示例2: prims
# 需要导入模块: from priorityQueue import PriorityQueue [as 别名]
# 或者: from priorityQueue.PriorityQueue import insert [as 别名]
def prims(graphObj, start):
pq = PriorityQueue()
for vertex in graphObj.verticesList:
if vertex == start:
g.verticesList[vertex].distance = 0
pq.insert([0, vertex])
continue
g.verticesList[vertex].distance = INFINITY
pq.insert([INFINITY, vertex])
while(len(pq.pqueue)):
currentVertex = pq.extractMin()
if len(pq.pqueue) == 1:
return
for adjNode in graphObj.verticesList[currentVertex[1]].getConnections():
if adjNode in pq.lookup(adjNode):
newDistance = graphObj.verticesList[currentVertex[1]].getCost(adjNode)
if newDistance < graphObj.verticesList[adjNode].distance:
graphObj.verticesList[adjNode].distance = newDistance
graphObj.verticesList[adjNode].predecessor = currentVertex[1]
return graphObj
示例3: triple
# 需要导入模块: from priorityQueue import PriorityQueue [as 别名]
# 或者: from priorityQueue.PriorityQueue import insert [as 别名]
rejectCount = 0
# Representation of a solution incumbent is a triple (score, revlist, result) where
# revlist is a list of reversals, result is the permutation after applying the
# reversals to the starting permutation, and score is the length of revlist
# plus the reversalBound of the result.
initialState = (reversalBound(alpha), [], alpha)
print "Initial state: ", initialState
# Build a list of all possible reversals.
allrevs = [(i,j) for i in xrange(n-1) for j in xrange(i+1, n)]
# The queue is implemented as a list of dictionaries. Each dictionary holds items of
# the same score (0...n-1).
queue = PriorityQueue(n)
queue.insert(initialState)
maxQueueLen = len(queue)
while True:
l = len(queue)
if l % 1000 <= 5:
print l
incumbent = queue.pop()
if incumbent == None: break
#print "Popping ", incumbent
if (incumbent[0] >= bestScore):
#print "Rejecting ", incumbent
continue
示例4: __init__
# 需要导入模块: from priorityQueue import PriorityQueue [as 别名]
# 或者: from priorityQueue.PriorityQueue import insert [as 别名]
"""
def __init__(self, name, score):
self.name = name
self.score = score
def __str__(self):
return "%-16s: %d" % (self.name, self.score)
def __cmp__(self, other):
if self.score < other.score: return 1 #lower score is better
if self.score > other.score: return -1
return 0 #if equal, return 0
""" Test Client """
tiger = Golfer("Tiger Woods", 61)
phil = Golfer("Phile Mickelson", 72)
hal = Golfer("Hal Sutton", 69)
pq = PriorityQueue()
pq.insert(tiger)
pq.insert(phil)
pq.insert(hal)
while not pq.is_empty(): print pq.remove()
#OUTPUT:
# Tiger Woods : 61
# Hal Sutton : 69
# Phil Mickelson : 72