Overview
Dijkstra’s algorithm is a greedy algorithm that solves the single-source shortest path problem for a weighted directed graph with non-negative edge weights. It efficiently computes the shortest distances from a starting vertex to all other vertices in the graph.How It Works
The algorithm maintains two sets of vertices:- Processed vertices - vertices for which the shortest distance has been finalized
- Unprocessed vertices - vertices still being evaluated
Algorithm Steps
Initialize distances
Set the distance to the start vertex as 0 and all other distances to infinity. Mark all vertices as unprocessed.
Select minimum distance vertex
From the unprocessed vertices, select the one with the minimum distance value.
Update adjacent vertices
For the selected vertex, examine all its unprocessed neighbors. Calculate their distance through the current vertex and update if this path is shorter.
Function Signature
Parameters
A 2D adjacency matrix representing the weighted directed graph.
graph[i][j] represents the weight of the edge from vertex i to vertex j. A value of 0 indicates no edge exists.The index of the starting vertex (0-based).
Returns
An array where
distances[i] represents the shortest distance from the start vertex to vertex i.Implementation
dijkstra.ts
Complexity Analysis
Time Complexity
O(V²) where V is the number of verticesCan be optimized to O(E log V) using a binary heap or priority queue, where E is the number of edges.
Space Complexity
O(V) for storing distances and processed status of all vertices
Complexity Breakdown
- Outer loop: Runs V times (once for each vertex)
- Finding minimum distance vertex: O(V) in the basic implementation
- Inner loop: Checks all V vertices for each selected vertex
- Overall: O(V) × O(V) = O(V²)
Usage Examples
Basic Example
Visualizing the Graph
The graph from the example above can be visualized as:Shortest Paths Explanation
Path A → B (distance: 2)
Path A → B (distance: 2)
Direct edge from A to B with weight 2.
Path A → C (distance: 4)
Path A → C (distance: 4)
Direct edge from A to C with weight 4 (shorter than A→B→C which is 2+2=4).
Path A → D (distance: 6)
Path A → D (distance: 6)
Path: A → B → E → D with total weight 2 + 2 + 3 = 7, but A → B → D gives 2 + 4 = 6.
Path A → E (distance: 4)
Path A → E (distance: 4)
Path: A → B → E with total weight 2 + 2 = 4.
Path A → F (distance: 6)
Path A → F (distance: 6)
Path: A → B → E → F with total weight 2 + 2 + 2 = 6.
City Navigation Example
Common Use Cases
GPS Navigation
Finding the shortest route between locations in mapping applications.
Network Routing
Determining optimal paths for data packets in computer networks.
Transportation
Optimizing delivery routes and minimizing travel costs.
Game Development
Pathfinding for NPCs and AI agents in games.
Key Characteristics
Greedy Algorithm: Makes the locally optimal choice at each step by always selecting the unprocessed vertex with the minimum distance.
Optimal Solution: Guarantees finding the shortest path when all edge weights are non-negative.
Single-Source: Computes shortest paths from one source vertex to all other vertices in a single execution.
Advantages & Limitations
Advantages
- Efficient for sparse graphs when using a priority queue
- Guarantees optimal solution for non-negative weights
- Computes all shortest paths from the source in one execution
- Well-studied with many optimizations available
Limitations
- Cannot handle negative edge weights
- O(V²) time complexity in basic implementation can be slow for dense graphs
- Requires all vertices to be known in advance
- May be overkill if only a single destination path is needed (consider A* algorithm)
Related Algorithms
Bellman-Ford
Handles negative edge weights but slower (O(VE) time complexity).
A* Search
Optimized version using heuristics for finding path to a specific target.
Floyd-Warshall
All-pairs shortest path algorithm with O(V³) time complexity.
BFS
Simpler alternative for unweighted graphs with O(V+E) time.
Further Reading
Wikipedia
Comprehensive overview and history of Dijkstra’s algorithm
Visualization
Interactive visualization of the algorithm