Refinement of the launch function of the Dijkstra algorithm

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

New contributor

Руслан Тыщук is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật