how to save shortest path in dijkstra algorithm
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Dijkstra's algorithm is a fundamental concept in algorithmic graph theory used to find the shortest path between nodes in a weighted graph. While its primary purpose is to calculate the shortest path distance, saving the actual path is often equally important. This article delves into techniques and methods to efficiently save and retrieve the shortest path using Dijkstra's algorithm.
Key Terminologies
Before diving into the implementation, let's clarify some essential terminologies:
- Graph: A collection of nodes (vertices) and edges connecting pairs of nodes.
- Weighted Graph: A graph where each edge has a numerical value or weight.
- Shortest Path: The path between two vertices that has the smallest sum of weights of edges.
Implementing Dijkstra's Algorithm
Dijkstra's algorithm works by iteratively exploring the unvisited node with the smallest tentative distance, updating the distances of neighboring nodes, and marking nodes as visited once processed.
Steps to Save the Shortest Path
- Initialization: Start with setting the distance to the source node as zero and infinity for others. Use a priority queue to efficiently fetch nodes with the smallest distance.
- Parent Array: Maintain a `parent` array where `parent[i]` keeps track of the immediate predecessor of node `i` on the shortest path from the source. This array is pivotal for path reconstruction.
- Relaxation: For each explored edge, if a shorter path is found to a neighboring node, update the distance and set the current node as its parent.
- Backtracking: Once the target node is processed, backtrack using the `parent` array to reconstruct the path.
Example Implementation
The following Python example demonstrates the concept:
- Time Complexity: , where is the number of vertices and is the number of edges. The priority queue operations are logarithmic.
- Space Complexity: , owing to the `distances`, `parent`, and priority queue requirements.
Related reading
- How to select points at a regular density
- How to select strategy to reduce overfitting?
- How to set patience as default git diff algorithm
- How to solve my difficulty making algorithms?
- How to save the memory when storing color information in Red-Black Trees?
- How to scale k8s pods according to rabbitmq queue message rate?
- How to scale down a UIImage and make it crispy / sharp at the same time instead of blurry?
- How to scale threads according to CPU cores?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.