- Joined
- Oct 28, 2023
- Messages
- 1
- Reaction score
- 0
import networkx as nx
import heapq
def dijkstra(graph, start, weight_property):
time_distances = {start: 0}
price_distances = {start: 0}
paths = {start: [start]}
queue = [(0, start)]
while queue:
current_dist, current_vertex = heapq.heappop(queue)
current_time = time_distances[current_vertex]
current_price = price_distances[current_vertex]
if current_dist > current_time:
continue
for neighbor, properties in graph[current_vertex].items():
weight = properties[weight_property]
new_dist = current_dist + weight
new_time = current_time + weight
new_price = current_price + weight
if neighbor not in time_distances or new_time < time_distances[neighbor]:
time_distances[neighbor] = new_time
if neighbor not in price_distances or new_price < price_distances[neighbor]:
price_distances[neighbor] = new_price
paths[neighbor] = paths[current_vertex] + [neighbor] # Store the path
heapq.heappush(queue, (new_dist, neighbor))
return time_distances, price_distances, paths
initial_city = input("Enter the initial city: ")
final_city = input("Enter the final city: ")
# Find shortest route based on travel time
travel_time_distances, travel_time_paths = dijkstra(combined_graph, initial_city, 'flight_time')
shortest_travel_time = travel_time_time_distances.get(final_city)
# Find shortest route based on price
price_distances, price_paths = dijkstra(combined_graph, initial_city, 'price')
cheapest_route = price_price_distances.get(final_city)
if shortest_travel_time:
travel_time_route = travel_time_paths[final_city]
travel_time = travel_time_distances[final_city]
travel_price = travel_time_distances[final_city]
print("Shortest route based on travel time:", travel_time_route,
travel_time // 60, "h", travel_time % 60, "m,", "Cost:", travel_price, "$")
else:
print("No route found based on travel time")
if cheapest_route:
price_route = price_paths[final_city]
cheapest_price = price_distances[final_city]
cheapest_price_time = price_distances[final_city]
print("Cheapest route:", price_route, cheapest_price, "$, Time:", cheapest_price_time // 60, "h", cheapest_price_time % 60, "m")
else:
print("No route found based on price")
Traceback (most recent call last):
File "c:\Users\lenovo\Desktop\khalbai_diar_project_bali\project_bali_diar\project_bali_diar.py", line 235, in <module>
travel_time_distances, travel_time_paths = dijkstra(combined_graph, initial_city, 'flight_time')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: too many values to unpack (expected 2)
import heapq
def dijkstra(graph, start, weight_property):
time_distances = {start: 0}
price_distances = {start: 0}
paths = {start: [start]}
queue = [(0, start)]
while queue:
current_dist, current_vertex = heapq.heappop(queue)
current_time = time_distances[current_vertex]
current_price = price_distances[current_vertex]
if current_dist > current_time:
continue
for neighbor, properties in graph[current_vertex].items():
weight = properties[weight_property]
new_dist = current_dist + weight
new_time = current_time + weight
new_price = current_price + weight
if neighbor not in time_distances or new_time < time_distances[neighbor]:
time_distances[neighbor] = new_time
if neighbor not in price_distances or new_price < price_distances[neighbor]:
price_distances[neighbor] = new_price
paths[neighbor] = paths[current_vertex] + [neighbor] # Store the path
heapq.heappush(queue, (new_dist, neighbor))
return time_distances, price_distances, paths
initial_city = input("Enter the initial city: ")
final_city = input("Enter the final city: ")
# Find shortest route based on travel time
travel_time_distances, travel_time_paths = dijkstra(combined_graph, initial_city, 'flight_time')
shortest_travel_time = travel_time_time_distances.get(final_city)
# Find shortest route based on price
price_distances, price_paths = dijkstra(combined_graph, initial_city, 'price')
cheapest_route = price_price_distances.get(final_city)
if shortest_travel_time:
travel_time_route = travel_time_paths[final_city]
travel_time = travel_time_distances[final_city]
travel_price = travel_time_distances[final_city]
print("Shortest route based on travel time:", travel_time_route,
travel_time // 60, "h", travel_time % 60, "m,", "Cost:", travel_price, "$")
else:
print("No route found based on travel time")
if cheapest_route:
price_route = price_paths[final_city]
cheapest_price = price_distances[final_city]
cheapest_price_time = price_distances[final_city]
print("Cheapest route:", price_route, cheapest_price, "$, Time:", cheapest_price_time // 60, "h", cheapest_price_time % 60, "m")
else:
print("No route found based on price")
Traceback (most recent call last):
File "c:\Users\lenovo\Desktop\khalbai_diar_project_bali\project_bali_diar\project_bali_diar.py", line 235, in <module>
travel_time_distances, travel_time_paths = dijkstra(combined_graph, initial_city, 'flight_time')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: too many values to unpack (expected 2)