Dijkstra's algorithm provides an efficient method for finding the shortest path from a given node to all other nodes in a directed, edge-weighted graph.
In the Dijkstra.py file, we have provided a program that includes an example graph.
The Graph class takes care of storing the graph. Its object variable graph stores the adjacency matrix in a NumPy array. The \([i,j]\)-th element of this matrix represents the weight of the edge from \(i\) to \(j\), or 0 if there is no edge from \(i\) to \(j\).
The task is to implement Dijkstra's algorithm in the dijkstra function of the Graph class. This function receives the index of the source vertex as a parameter. Its return value should be an array where the \(j\)-th element is the weight of the shortest path from the source to vertex \(j\). If necessary, additional helper functions can be added to the class.
We provide the source vertex to the program via the command line, for example:
python3 Dijkstra.py 4if we are interested in paths starting from vertex 4. After execution, the program lists the weight of the shortest path to every vertex.