Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i am implementing a program to find the shortest path between two cities using digestra algorithm with hep in python and the last step for

i am implementing a program to find the shortest path between two cities using digestra algorithm with hep in python and the last step for me is how can i instead of inserting the data of the cities and distance between them manually i want to link the program with a file that have most of the cities as an example is turkey and retrive the shortest path depending on what the user choose but i dont have any idea of how to do that this is a copy of my code if you want to check it out import customtkinter import networkx as nx import matplotlib.pyplot as plt from collections import defaultdict from heapq import heappop, heappush customtkinter.set_appearance_mode ("dark") customtkinter.set_default_color_theme("dark-blue") root = customtkinter.CTk() root.title("Shortest Path") root.geometry("500x350") def buttonAction(): # Create a directed graph using the given data G = nx.DiGraph() G.add_edges_from([('Istanbul', 'Ankara', {'weight': 2}), ('Istanbul', 'Izmir', {'weight': 4}), ('Ankara', 'Izmir', {'weight': 3}), ('Ankara', 'Bursa', {'weight': 8}), ('Izmir', 'Bursa', {'weight': 2}), ('Izmir', 'Gaziantep', {'weight': 5}), ('Bursa', 'Gaziantep', {'weight': 11}), ('Bursa', 'Konya', {'weight': 22}), ('Gaziantep', 'Konya', {'weight': 1})]) def dijkstra(graph, source, destination): # Initialize distances and previous nodes dist = defaultdict(lambda: float('inf')) prev = defaultdict(lambda: None) dist[source] = 0 # Initialize priority queue queue = [(0, source)] # Keep track of visited nodes visited = set() # Iterate until we have no more nodes to explore while queue: # Explore the next node dist_node, node = heappop(queue) if node in visited: continue visited.add(node) # Update distances and previous nodes for neighbors for neighbor, attr in graph[node].items(): weight = attr['weight'] if dist[neighbor] > dist_node + weight: dist[neighbor] = dist_node + weight prev[neighbor] = node heappush(queue, (dist[neighbor], neighbor)) # Construct the shortest path path = [] node = destination while node is not None: path.append(node) node = prev[node] return path[::-1], dist[destination] # Use Dijkstra's algorithm to find the shortest path source = comboBox_startnode.get() destination = comboBox_endnode.get() shortest_path, shortest_distance = dijkstra(G, source, destination) # Visualize the graph pos = nx.spring_layout(G) nx.draw(G, pos, with_labels=True, font_weight='bold') # Color the shortest path red nx.draw_networkx_nodes(G, pos, nodelist=shortest_path, node_color='r') # Show edge labels labels = nx.get_edge_attributes(G,'weight') nx.draw_networkx_edge_labels(G, pos, edge_labels=labels) plt.show() frame = customtkinter.CTkFrame(master=root) frame.pack(pady=20, padx=60, fill="both", expand=True) label = customtkinter.CTkLabel(master=frame, text="Find The Shortest Path Route!") label.pack(pady=12, padx=10) comboBox_startnode = customtkinter.CTkComboBox(master=frame, values=["Istanbul"]) comboBox_startnode.pack(pady=12, padx=10) comboBox_endnode = customtkinter.CTkComboBox(master=frame, values=["Konya"]) comboBox_endnode.pack(pady=12, padx=10) button = customtkinter.CTkButton (master=frame, text="Find the Route", command=buttonAction) button.pack(pady=12, padx=10) root.mainloop()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part I Lnai 8724

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448475, 978-3662448472

More Books

Students also viewed these Databases questions

Question

Q.No.1 Explain Large scale map ? Q.No.2 Explain small scale map ?

Answered: 1 week ago