It is necessary to calculate the shortest path between two points and show it. I managed to calculate, but I can’t show myself the way.

from pygraph.classes.graph import graph from pygraph.algorithms.searching import * gr = graph() gr.add_nodes(['A','B','C','X','Y','Z']) # Add nodes # Add edges gr.add_edge(('X','Y'), 2) gr.add_edge(('X','Z'), 1) gr.add_edge(('A','B'), 1) gr.add_edge(('A','C'), 3) gr.add_edge(('Y','B'), 4) gr.add_edge(('Y','C'), 7) sh1, sh2 = shortest_path(gr, source = 'X'); print(sh2['A']) #кратчайшее расстояние от X до A 

7

In addition to the number 7, I need to get something like:

X -> Y -> B -> A

I am sure that the pygraph module allows you to do this, but I cannot correctly navigate - I shoveled the entire module, but I found what I was looking for.

    1 answer 1

    On this page, the description of the function shortest_path (graph, source).