There is the following code to output the result of the algorithm:
def print_result(previous_nodes, shortest_path, start_node, target_node):
path = []
node = target_node
while node != start_node:
path.append(node)
node = previous_nodes[node]
# Добавить начальный узел вручную
path.append(start_node)
result_text = "Найден следующий лучший маршрут с расстоянием {} км".format(shortest_path[target_node])
result_text += "n" + " -> ".join(reversed(path))
# Вставить результат в текстовое поле
text_input.insert(END, result_text + "n")
It is designed to display the result of the work in the following form:
“The next best route with a distance of 56.02 km has been found
Warehouse -> Nekrasovskaya -> 3rd Working -> FEFU
The next best route with a distance of 22.78 km has been found
FEFU -> 3rd Working -> VVSU
The next best route with a distance of 6.11 km has been found
VVSU -> TSMU”
There is also the code of the function that runs this algorithm:
def run_dijkstra():
global selected_value
global selected_value1
global selected_value2
route = []
while array:
min_distance = float('inf')
closest_stop = None
for stop in array:
graph = Graph(nodes, init_graph)
previous_nodes, shortest_path = dijkstra_algorithm(graph=graph, start_node=selected_value)
distance = shortest_path[stop]
# Если найдено более короткое расстояние, обновляем ближайшую остановку
if distance < min_distance:
min_distance = distance
closest_stop = stop
route.append(closest_stop)
array.remove(closest_stop)
# Обновляем стартовую точку для следующего шага
selected_value = closest_stop
print(route)
It is designed to ensure that the array “array”, in which we manually enter stops, is displayed in the text field so that the stops go based on which one is closer to the starting position.
The problem is that the “run_dijkstra” function outputs the result as an array, but it needs to output the result using the “print_result” function.
In its current form, the “run_dijkstra” function outputs the result simply as an array:
[FEFU, VGUES, TSMU]
And I need it to output the result as above, using the “print_result” function.
Initially, the “run_dijkstra” function looked like this:
def run_dijkstra():
global selected_value
global selected_value1
global selected_value2
route_value = 0
if selected_value:
for index, znach in enumerate(array):
previous_index = index -1
if index == 0:
graph = Graph(nodes, init_graph)
previous_nodes, shortest_path = dijkstra_algorithm(graph=graph, start_node= selected_value)
print_result(previous_nodes, shortest_path, start_node= selected_value, target_node=znach)
route_value += shortest_path[znach]
elif index < len(array):
previous_znach = array[previous_index]
graph = Graph(nodes, init_graph)
previous_nodes, shortest_path = dijkstra_algorithm(graph=graph, start_node= previous_znach)
print_result(previous_nodes, shortest_path, start_node= previous_znach, target_node=znach)
route_value += shortest_path[znach]
It just used the “print_result” function and it output the result as needed, and the loop was made so that all the elements of the entered array were output in turn.
But later I needed the array elements to be displayed based on which one is located closer to the starting point, but I don’t know how to combine the new modified “run_dijkstra” and “print_result” functions.
This is where I need your help
Руслан Тыщук is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.