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


Python PriorityQueue.pop_task方法代碼示例

本文整理匯總了Python中PriorityQueue.PriorityQueue.pop_task方法的典型用法代碼示例。如果您正苦於以下問題:Python PriorityQueue.pop_task方法的具體用法?Python PriorityQueue.pop_task怎麽用?Python PriorityQueue.pop_task使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在PriorityQueue.PriorityQueue的用法示例。


在下文中一共展示了PriorityQueue.pop_task方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: Prim

# 需要導入模塊: from PriorityQueue import PriorityQueue [as 別名]
# 或者: from PriorityQueue.PriorityQueue import pop_task [as 別名]
def Prim(G, w, s):
	inf = float("inf")
	H = PriorityQueue()
	T = []
	s.priorite = 0
	H.add_task(s, 0)
	i = 1
	for u in G.sommets:
		u.pred = None
		if u != s :
			H.add_task(u, inf)
			u.priorite = inf
	try:
		while True:
			u = H.pop_task()
			u.couleur = "noir"
			if u.priorite != inf:
				if u.pred is not None: T.append((u.pred, u))
				for v in u.voisins:
					if v.couleur == "blanc": #v n'est pas sorti du tas
						if w[(u, v)] < v.priorite:
							v.priorite = w[(u, v)]
							H.add_task(v, v.priorite)
							v.pred = u
	except:
		pass
	return T
開發者ID:XAMEUS,項目名稱:AL5,代碼行數:29,代碼來源:Prim.py

示例2: Prims

# 需要導入模塊: from PriorityQueue import PriorityQueue [as 別名]
# 或者: from PriorityQueue.PriorityQueue import pop_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.pop_task方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。