Exemplo n.º 1
0
def Prim(graph, start):
    start.setDistance(0)
    pq = PriorityQueue()
    pq.buildHeap([(v.getDistance(), v) for v in graph])

    while not pq.isEmpty():
        curNode = pq.delMin()
        for nbr in curNode.getConnections():
            newDist = curNode.getDistance() + curNode.getWeight(nbr)
            if nbr in pq and newDist < newDist.getDistance():
                nbr.setDistance(newDist)
                nbr.setPred(curNode)
                pq.decreaseKey(nbr, newDist)
Exemplo n.º 2
0
def prim(G, start):
    pq = PriorityQueue()
    start.setDistance(0)
    #  O(V)
    pq.buildHeap([(v.getDistance(), v) for v in G])

    while not pq.isEmpty():
        closest = pq.delMin()
        for n in closest.getConnections():
            newCost = closest.getWeight(n)
            if n in pq and newCost < n.getDistance():
                n.setPred(closest)
                n.setDistance(newCost)
                pq.decreaseKey(n, newCost)

    return G
Exemplo n.º 3
0
def dijkstras_pq(g, start):
    pq = PriorityQueue()
    start.setDistance(0)
    # O(V)
    pq.buildHeap([(v.getDistance(), v) for v in g])

    while not pq.isEmpty():
        # O(logV) * O(V) = O(V*logV)
        closest = pq.delMin()
        for n in closest.getConnections():
            new_distance = closest.getDistance() + closest.getWeight(n)
            if new_distance < n.getDistance():
                n.setDistance(new_distance)
                n.setPred(closest)
                # O(E*logV)
                pq.decreaseKey(n, new_distance)
Exemplo n.º 4
0
def dijkstra(aGraph:Graph,start:Vertex):
    pq = PriorityQueue()
    start.setDistance(0)
    pq.buildHeap([(v.getDistance(),v) for v in aGraph])

    # while not pq.isEmpty():
    while pq.currentSize > 1:  # 保留优先队列一个值的原因是优先队列的初始化时会先加一个(0,0)元组,用处是便于计算堆的索引

        curV = pq.delMin()
        for nextV in curV.getConnections():
            newDist = curV.getDistance() + curV.getWeight(nextV)
            if newDist < nextV.getDistance():
                nextV.setDistance(newDist)
                nextV.setPred(curV)
                pq.decreaseKey(nextV,newDist)   #这个方法应该是内部调用了percUp(),有调整堆的味道
Exemplo n.º 5
0
def Prim(aGraph:Graph,start:Vertex):
    pq = PriorityQueue()
    start.setDistance(0)
    pq.buildHeap([(v.getDistance(),v) for v in aGraph])

    res = []
    # while not pq.isEmpty():
    while pq.currentSize > 1:   #保留优先队列一个值的原因是优先队列的初始化时会先加一个(0,0)元组,用处是便于计算堆的索引
        curV = pq.delMin()
        for nbr in curV.getConnections():
            cost = curV.getDistance() + curV.getWeight(nbr)
            if nbr in pq and cost < nbr.getDistance():
                nbr.setDistance(cost)
                nbr.setPred(curV)
                pq.decreaseKey(nbr,cost)
        res.append(curV.id)
    print(res)
Exemplo n.º 6
0
def MinSpaningTree(graph, start_vertex):
    ## We will push the entire graph in a Priority Queue
    fringe = PriorityQueue()
    ## set distances of each vertex except starting vertex to Inf
    for v in graph.getVertices():
        vert = graph.getVertex(v)
        vert.setDistance(inf)
        vert.setParent(None)
    graph.getVertex(start_vertex).setDistance(0)
    fringe.buildHeap([(graph.getVertex(v).getDistance(), v)
                      for v in graph.getVertices()])
    while not fringe.isEmpty():
        node = fringe.delMin()
        ## update value of all connected vertices of node
        for x in graph.getVertex(node).connection:
            vert = graph.getVertex(x)
            new_cost = graph.getVertex(node).connection[x]
            if x in fringe and new_cost < vert.getDistance():
                vert.setDistance(new_cost)
                vert.setParent(node)
                fringe.decreaseKey(x, new_cost)
 def dijkstra_end(self, start, end):
     distance = {node: inf for node in self.graph}
     visited = {node: False for node in self.graph}
     pq = PriorityQueue()
     distance[start] = 0
     pq.add((0, start))
     for next_node in self.graph[start]:
         initial_weight = self.weights[(start.num, next_node.num)]
         pq.add((initial_weight, next_node))
     while not pq.isEmpty():
         currentVert = pq.delMin()
         visited[currentVert] = True
         if (visited[end]):
             break
         for nextVert in self.graph[currentVert]:
             newDist = distance[currentVert] \
                 + self.weights[(currentVert.num, nextVert.num)]
             if newDist < distance[nextVert]:
                 distance[nextVert] = newDist
                 pq.decreaseKey(nextVert, newDist)
                 pq.add((newDist, nextVert))
     if not visited[end]:
         return None
     return distance[end]
# print(atividades.atividades)
# print("-------------------------------------")
sortedAtividades = []
for i in atividades.atividades:
    sortedAtividades.append(
        sorted(atividades.atividades[i], key=lambda x: x.horarioInicial))
# print(sortedAtividades)

""" for i in sortedAtividades:
    count += 1
    print(daySelector.selectDia(count))
    for j in i:
        print(j) """
count = 0
for j in sortedAtividades:
    funcionarios = PriorityQueue()
    qtd_funcionarios = 0
    atividadesDia = {}
    count += 1
    for i in j:
        if not funcionarios.isEmpty():
            disponivel = funcionarios.delMin()
            #print (disponivel)

        else:
            disponivel = None

        if disponivel is not None:
            if i.horarioInicial >= disponivel[0]:
                funcionarios.add(
                    (i.horarioFinal, (i.horarioFinal, disponivel[1])))