Exemplo n.º 1
0
def handle_route_information_option(dict_of_edges, dict_of_cities, graph):
    """
    Handles queries asking for route information for a given complete route (inputted from the console)
    :param dict_of_edges:
    :param dict_of_cities:
    :param graph:
    :return: Nothing
    """
    print("ROUTE INFORMATION")
    print("------------------")
    print("Enter the subsequent cities and enter 'STOP' when you're done entering.\n")
    locations = []
    current_city = input()
    while not current_city.upper() == 'STOP':

        current_city = validate_city_name_or_code(current_city, dict_of_cities)
        if not current_city:
            return

        locations.append(current_city)
        current_city = input()

    print("Route:- ")
    for city in locations:
        print(city, end = "-->")
    print("||")

    Edge.getRouteInformation(dict_of_edges,graph,locations)
Exemplo n.º 2
0
def find_shortest_path(graph, source, destination, dict_of_edges):
    """
    Uses DIJKSTRA'S ALGORITHM TO GET THE SHORTEST PATH
    :param graph:
    :param source:
    :param destination:
    :param dict_of_edges: to later find out the route information
    :return:
    """
    dist = {}
    prev = {}
    unvisited = []
    found = False
    for key in graph:
        dist[key] = 999999
        prev[key] = "NA"
        unvisited.append(key)

    dist[source] = 0

    while len(unvisited) > 0:
        u = unvisited[0]
        for city in unvisited:
            if dist[city]<dist[u]:
                u = city
        unvisited.remove(u)
        if u==destination:
            found = True
            break

        for neighbour in graph[u]:
            neighbour_key = neighbour[0]
            neighbour_distance = neighbour[1]
            alt = dist[u] + neighbour_distance
            if alt < dist[neighbour_key]:
                dist[neighbour_key] = alt
                prev[neighbour_key] = u
    stack_of_path = []
    if found:
        previous = destination
        while not previous == source:
            stack_of_path.append(previous)
            # print(previous, end  = "<--")
            previous = prev[previous]
    else:
        print("No path between ends found!")

    if found:
        locations = []
        stack_of_path.append(source)
        print("\nShortest path is:\n")
        while len(stack_of_path) > 1:
            city_reached = stack_of_path.pop()
            print(city_reached, end="-->")
            locations.append(city_reached)
        print(destination)
        locations.append((destination))
        Edge.getRouteInformation(dict_of_edges,graph,locations)