I need to implement Dijkstra’s Algorithm in Python. However, I have to use a 2D array to hold three pieces of information – predecessor, length and unvisited/visited.
I know in C a Struct can be used, though I am stuck on how I can do a similar thing in Python, I am told it’s possible but I have no idea to be honest
I need to implement Dijkstra’s Algorithm in Python. However, I have to use a
Share
As mentioned above, you can use an instance of an object.
This author has a pretty convincing python implementation of Dijkstras in python.
Notice those pieces of information are ‘held’ in the object he is constructing by calling Algorithms.Entry(). Entry is a class and is defined like this:
The self.known, self.distance… are those pieces of information. He does not set these explicit in the constructor (init) but sets them later. In Python you can access attributes with dot notation. for examle: myObject= Entry(). the myObject.known, myObject.distance… they are all public.