当前位置: 首页>>代码示例>>Python>>正文


Python PriorityQueue.insert方法代码示例

本文整理汇总了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
开发者ID:AjithPanneerselvam,项目名称:Algorithm,代码行数:28,代码来源:dijkstra.py

示例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
开发者ID:AjithPanneerselvam,项目名称:Algorithm,代码行数:27,代码来源:prims.py

示例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
开发者ID:,项目名称:,代码行数:32,代码来源:

示例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
开发者ID:angelgao,项目名称:Algorithms,代码行数:32,代码来源:priorityQueueTestClient.py


注:本文中的priorityQueue.PriorityQueue.insert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。