DEV Community

Discussion on: Dijkstra's algorithm in python: algorithms for beginners

Collapse
 
nick9951 profile image
nick9951

If we want to know the shortest path and total length at the same time
how to change the code?

Collapse
 
buecktobias profile image
buecktobias

I'd like to know that too.

Collapse
 
ruidazeng profile image
Ruida Zeng

Using Python object-oriented knowledge, I made the following modification to the dijkstra method:

return the distance between the nodes

    distance_between_nodes = 0
    for index in range(1, len(path)):
        for thing in self.edges:
            if thing.start == path[index - 1] and thing.end == path[index]:
                distance_between_nodes += thing.cost
    return distance_between_nodes       
    # return path