What is the role of a predecessor array in the Dijkstra Algorithm?

Dijkstra Algorithm Questions Medium



80 Short 62 Medium 80 Long Answer Questions Question Index

What is the role of a predecessor array in the Dijkstra Algorithm?

The predecessor array in the Dijkstra Algorithm is used to keep track of the shortest path from the source vertex to each vertex in the graph. It stores the immediate predecessor of each vertex along the shortest path found so far.

Initially, all vertices in the graph are assigned a distance value of infinity, except for the source vertex which is assigned a distance value of 0. As the algorithm progresses, the distances are updated and the predecessor array is updated accordingly.

During the execution of the algorithm, when a shorter path to a vertex is found, the distance value of that vertex is updated and its predecessor is set to the vertex from which the shorter path was found. This process continues until all vertices have been visited and the shortest path from the source vertex to each vertex has been determined.

The predecessor array is crucial in reconstructing the shortest path from the source vertex to any other vertex in the graph. By following the predecessors from the destination vertex back to the source vertex, the shortest path can be obtained.