示例#1
0
def TCPSimulator():
    """
    This function implements the test program for the assignment.
    It begins by creating the client and server objects and then
    executes an event loop driven by a priority queue for which
    the priority value is time.
    """
    eventQueue = PriorityQueue()
    client = TCPClient()
    server = TCPServer()
    client.server = server
    server.client = client
    client.queueRequestMessage(eventQueue, 0)
    while not eventQueue.isEmpty():
        e, t = eventQueue.dequeueWithPriority()
        if EVENT_TRACE:
            print(str(e) + " at time " + str(t))
        e.dispatch(eventQueue, t)
示例#2
0
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)
示例#3
0
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)
示例#4
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)