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


Python PriorityQueue.remove_task方法代码示例

本文整理汇总了Python中PriorityQueue.PriorityQueue.remove_task方法的典型用法代码示例。如果您正苦于以下问题:Python PriorityQueue.remove_task方法的具体用法?Python PriorityQueue.remove_task怎么用?Python PriorityQueue.remove_task使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PriorityQueue.PriorityQueue的用法示例。


在下文中一共展示了PriorityQueue.remove_task方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Prims

# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import remove_task [as 别名]
def Prims( graph, start ):
    ''' Creates a minimum  spanning tree using Prims algorithm
    '''
    print "Start vertex = ", start

    spanningTree = Graphs.Graph( graph.maxVertices, False )
    inTree = [False]*graph.maxVertices
    inTree[start] = True
    spanningVertices = []   #[0]*graph.maxVertices
    spanningVertices.append(start)

    # Initialize the heap (PriorityQueue) with the min edge of the first
    # vertex for its key, and ininity for all other keys (vertexs).
    heapQueue = PriorityQueue()
    #for vertex in range(0, len(graph.vertices)):
    #    if( not (graph.vertices[vertex]).empty ):
    #        heapQueue.add_task(vertex, 35850023)    #sys.maxint)

    startEdges = (graph.vertices[start]).priorityEdges
    nextVertex, minWeight, noMoreValidEdges = startEdges.pop_task()
    heapQueue.add_task( (start, nextVertex), minWeight)
    totalCost = 0

    while( not noMoreValidEdges ):

        # Pop the next cheapest edge off the list off the heap (prioirty queue) (this will remove it)
        nextEdge, cheapestWeight, noMoreValidEdges = heapQueue.pop_task()

        if( not noMoreValidEdges ):
            nextVertex = nextEdge[1]
            vertexInTreeGettingUpdate = nextEdge[0]
            if( not inTree[nextVertex] ):
                #print "Next vertex, weight, total = ", nextVertex, cheapestWeight, totalCost
                totalCost += cheapestWeight
                inTree[nextVertex] = True
                spanningVertices.append(nextVertex)
                spanningTree.InsertEdge(vertexInTreeGettingUpdate, nextVertex, False, cheapestWeight)

                # Need to find the cheapest edges for both vertex's on the next edge that is
                # coming into the spanning tree.
                # Note that the edge that led to this vertex being selected has now been
                # removed from the "priorityEdges" for that vertex by the "pop_task()" function below
                nextEdges = (graph.vertices[nextVertex]).priorityEdges
                edgesEmpty = False
                cheapestEdgeVertex = nextVertex
                # We keep popping the edges until we find one that's not already in the tree.
                while( (not edgesEmpty) and (inTree[cheapestEdgeVertex])):
                    cheapestEdgeVertex, cheapestEdgeWeight, edgesEmpty = nextEdges.pop_task()
                    #if( (not edgesEmpty) and (inTree[cheapestEdgeVertex])):
                    #    print "edge discarded because already in tree = ", cheapestEdgeVertex, cheapestEdgeWeight
                if( not edgesEmpty ):
                    heapQueue.remove_task( (nextVertex, cheapestEdgeVertex) )
                    heapQueue.add_task((nextVertex, cheapestEdgeVertex), cheapestEdgeWeight)

                nextEdges = (graph.vertices[vertexInTreeGettingUpdate]).priorityEdges
                edgesEmpty = False
                cheapestEdgeVertex = vertexInTreeGettingUpdate
                # We keep popping the edges until we find one that's not already in the tree.
                while( (not edgesEmpty) and (inTree[cheapestEdgeVertex])):
                    cheapestEdgeVertex, cheapestEdgeWeight, edgesEmpty = nextEdges.pop_task()
                    #if( (not edgesEmpty) and (inTree[cheapestEdgeVertex])):
                    #    print "edge discarded because already in tree = ", cheapestEdgeVertex, cheapestEdgeWeight
                if( not edgesEmpty ):
                    heapQueue.remove_task( (vertexInTreeGettingUpdate, cheapestEdgeVertex) )
                    heapQueue.add_task( (vertexInTreeGettingUpdate, cheapestEdgeVertex), cheapestEdgeWeight)

    return totalCost
开发者ID:QualityCodePassion,项目名称:Uni-Alg,代码行数:69,代码来源:Prims.py


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