Question
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started