Dijkstra's Algorithm
Dijkstra's algorithm finds the shortest paths from a source node to all other nodes in a weighted graph with non-negative edge weights.
AdvancedTime: O((V + E) log V)Space: O(V)
Take QuizStep 1 / 0
Current Step
Click Play to start the visualization
## How Dijkstra's Algorithm Works
Dijkstra's algorithm finds the shortest path from a source node to all other nodes in a weighted graph with non-negative edge weights.
### Algorithm Steps:
1. Set distance to source = 0, all others = infinity
2. Add all nodes to unvisited set
3. While unvisited set is not empty:
- Select node with minimum distance
- For each neighbor, calculate new distance through current node
- Update if new distance is shorter
4. Repeat until all nodes are visited
### Key Characteristics:
- **Greedy**: Always picks the closest unvisited node
- **Optimal**: Guarantees shortest path
- **Non-negative weights only**: Doesn't work with negative edges
Time & Space Complexity
| Case | Time Complexity |
|---|---|
| Best | O((V + E) log V) |
| Average | O((V + E) log V) |
| Worst | O(V²) |
| Space | O(V) |
Practice Problems
10 problems81 total points
1
Network Delay Time
Beginner5 pts
2
Path with Minimum Effort
Beginner5 pts
3
Cheapest Flights Within K Stops
Beginner5 pts
4
Shortest Path to Get All Keys
Intermediate8 pts
5
Swim in Rising Water
Intermediate8 pts
6
Path with Maximum Probability
Advanced10 pts
7
Minimum Cost to Make at Least One Valid Path
Advanced10 pts
8
Find the City With Smallest Number of Neighbors
Advanced10 pts
9
Minimum Time to Visit a Cell In a Grid
Advanced10 pts
10
Second Minimum Time to Reach Destination
Advanced10 pts