def applyDijkstra(g, start, finish):
    """
    Applies Dijkstra's algorithm to the graph g, updating the
    distance from start to each node in g.
    """
    #MAKE SURE THAT THE ALGORITHM IS APPLIED ONLY TO THE START/FINISH NODE
    #USE node.predecessor, node.distance

    #initializeSingleSource(g, start)

    finalized = set()
    frontier = PriorityQueue()
    finishedendnode = False
    #explored = set()
    start.distance = 0
    start.predecessor = None

    frontier.enqueue(start)

    while not frontier.isEmpty() and finishedendnode == False:
        node = frontier.dequeue()
        #print(type(node))
        finalized.add(node)
        if node == finish:
            finishedendnode = True
            break
        for arc in node.getArcsFrom():
            n1 = arc.getStart()
            n2 = arc.getFinish()
            #print("n1: "+ str(n1))
            #print("n2: "+ str(n2))
            #print("node: "+ str(node))
            if n2 not in finalized:
                #print("n2: "+ str(n2))
                if n2 not in frontier:
                    initIndivNode(n1, n2, arc)
                    frontier.enqueue(n2, n2.distance)
                #init single source
                else:
                    oldDistance = n2.distance
                    relax(n1, n2, arc.getCost())
                    if n2.distance < oldDistance:
                        frontier.raisePriority(n2, n2.distance)
def applyDijkstraOrig(g, start):
    """
    Applies Dijkstra's algorithm to the graph g, updating the
    distance from start to each node in g.
    """
    initializeSingleSource(g, start)
    finalized = set()
    pq = PriorityQueue()
    for node in g.getNodes():
        pq.enqueue(node, node.distance)
    while not pq.isEmpty():
        node = pq.dequeue()
        finalized.add(node)
        for arc in node.getArcsFrom():
            n1 = arc.getStart()
            n2 = arc.getFinish()
            if n2 not in finalized:
                oldDistance = n2.distance
                relax(n1, n2, arc.getCost())
                if n2.distance < oldDistance:
                    pq.raisePriorityOrig(n2, n2.distance)
Exemple #3
0
def applyDijkstra(g, start, finish=None):
    """
    Applies Dijkstra's algorithm to the graph g, updating the
    distance from start to each node in g.
    """
    initializeSingleSource(g, start)
    finalized = set()
    pq = PriorityQueue()
    pq.enqueue(start)
    while not pq.isEmpty():
        node = pq.dequeue()
        finalized.add(start)
        if node == finish:  #if we are at the finish node then we are all done!
            break
        for arc in node.getArcsFrom():
            n1 = arc.getStart()
            n2 = arc.getFinish()
            if n2 not in finalized:
                if n2.distance is math.inf:  #check if the node is one that we have not visited before, in which case we will add it to the queue
                    pq.enqueue(n2)
                oldDistance = n2.distance
                relax(n1, n2, arc.getCost())
                if n2.distance < oldDistance:
                    pq.raisePriority(n2, n2.distance)