Is Dijkstra's algorithm dynamic programming?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Dijkstra's algorithm is often brought up in discussions on algorithms related to graph theory and shortest paths. A common question is whether Dijkstra's algorithm can be considered an instance of dynamic programming. Let's delve into the details to address this query.
Understanding Dijkstra's Algorithm
Dijkstra's algorithm is a classic algorithm used to find the shortest path between nodes in a graph, which may represent, for example, road networks. It operates by iteratively selecting the node with the least total cost from a set of nodes, updating the cost to its neighbors, and marking it as completed (or visited).
Steps of Dijkstra's Algorithm
- Initialize: Set the distance to the starting node to zero and all other distances to infinity. Mark all nodes as unvisited. The starting node is added to a priority queue.
- Relaxation: While there are unvisited nodes:
- Extract the node with the minimum distance value.
- Update the distance values for its neighbors.
- If a shorter path to the neighbor is found, update its cost and add the neighbor to the queue if it's not there already.
- Repeat until the shortest path to the destination node is found or all nodes have been visited.
Example
Consider a graph where node A connects to nodes B and C, with edge weights 1 and 4, respectively. Applying Dijkstra's algorithm to find the shortest path from A to C involves the following:
- Initialization sets distances: , , .
- Relax edges from A: , .
- Select B (smallest distance), and since B doesn’t lead to C with a shorter path, proceed with C.
- No further updates are possible, producing the shortest path A → B → C with a cost of 3, assuming an edge from B to C with weight 2 exists.
Dynamic Programming Basics
Dynamic programming (DP) is a method for solving complex problems by breaking them down into simpler subproblems. It is applicable when the problem can be solved by combining the solutions to subproblems, often characterized by overlapping subproblems and optimal substructure.
Dynamic Programming Properties
- Optimal Substructure: A problem has an optimal substructure if the solution can be constructed efficiently from solutions of its subproblems.
- Overlapping Subproblems: Reusable subproblems whose solutions might be needed multiple times.
Classic DP Example: Fibonacci Numbers
Calculating Fibonacci numbers using DP involves storing the results of subproblems in a table to optimize repeated calculations. This contrasts with a naive recursive approach that redundantly solves the same subproblems.
Comparisons and Contrasts
While both Dijkstra's algorithm and dynamic programming involve breaking a problem into simpler pieces, there are key differences:
| Aspect | Dijkstra's Algorithm | Dynamic Programming |
| Problem Type | Shortest path in graphs | Optimizing problems with overlapping subproblems |
| Subproblem Reuse | No explicit reuse of subproblem solutions | Explicit overlapping subproblems |
| Data Structure | Priority queue for selecting the next node | Arrays or matrices for storing intermediate results |
| Nature | Greedy approach, prioritizes local optimum | Recursively solves and stores intermediate solutions |
Is Dijkstra's Algorithm Dynamic Programming?
To determine if Dijkstra's algorithm is an example of DP, we need to examine if it exhibits overlapping subproblems and optimal substructure.
- Optimal Substructure: Dijkstra's does possess optimal substructure. At each step, the shortest known path to a vertex is potentially built on the shortest paths that have already been established.
- Overlapping Subproblems: Unlike traditional dynamic programming techniques like the Fibonacci sequence or Knapsack problem, Dijkstra's algorithm does not solve the same subproblem multiple times; it calculates the shortest path once for each pair of nodes.
In conclusion, while Dijkstra's algorithm shares the optimal substructure property common to dynamic programming solutions, it does not explicitly feature overlapping subproblems, as the shortest path for each node is computed once and never revisited in the same manner as DP problems. Instead, Dijkstra's algorithm is more accurately characterized as a greedy algorithm due to its strategy of choosing the locally optimal choice (shortest edge) at each step with the hope of finding the global optimum.
This understanding highlights a nuanced distinction: although some algorithms can exhibit properties of dynamic programming, they may not fully qualify as dynamic programming solutions. Dijkstra’s algorithm, therefore, straddles the lines of both methods but is primarily recognized as a greedy algorithm in the context of shortest path problems.
Related reading
- Is Dijkstra's algorithm for directed or undirected graphs?
- Is Dynamic 0/1 Knapsack a Total Joke?
- Is golden section search better than binary search?
- Is it always possible to turn one BST into another using tree rotations?
- Is Disney's FastPass Valid and/or Useful Queue Theory
- Is int a reference type or a value type?
- Is it correct to ask to solve an NP-complete problem on a job interview?
- Is it faster to sort a list after inserting items or adding them to a sorted list

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.