DEV Community


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

ronozoro profile image
Mostafa Mohamed • Edited

This algorithm is working correctly only if the graph is directed,but if the graph is undireted it will not.

To make the algorithm work as directed graph you will have to edit neighbour function as

def neighbours(self):
    neighbours = {vertex: set() for vertex in self.vertices}
    for edge in self.edges:
        neighbours[edge.start].add((edge.end, edge.cost))
        neighbours[edge.end].add((edge.start, edge.cost))
    return neighbours
Forem Open with the Forem app