How does Dijkstra's Algorithm and A-Star compare?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In the realm of computer science, particularly in graph theory and pathfinding problems, Dijkstra's Algorithm and A* (A-Star) are two of the most prominent algorithms used to find the shortest path between two nodes in a graph. Both algorithms have their particular strengths and weaknesses, and understanding these can help in choosing the most appropriate one for a given problem. Let's explore how they compare.
Dijkstra's Algorithm
Dijkstra's Algorithm is a classic algorithm used for finding the shortest paths from a single source vertex to all other vertices in a graph. It was conceived by Edsger Dijkstra in 1956 and is renowned for its efficiency and versatility.
How It Works
The algorithm starts at the selected node (the "source") and explores all its neighboring nodes. It assigns each neighboring node a tentative distance value, which is the sum of the distance from the source node to that neighbor and any previously calculated shortest distance from the source to the current node. If this newly calculated distance is less than the current assigned value of that neighbor, it is updated. This process continues, visiting nodes and updating distances, until all nodes have been processed.
Key Characteristics
- Complexity: The time complexity of Dijkstra's algorithm is , where is the number of vertices. However, with a priority queue (using a binary heap), this can be reduced to , where is the number of edges.
- Optimality: Always provides the shortest path in graphs with non-negative edge weights.
- Application: Widely used in network routing protocols such as OSPF (Open Shortest Path First).
A* (A-Star) Algorithm
A* is an extension of Dijkstra's that improves its efficiency using heuristics. Developed in 1968 by Peter Hart, Nils Nilsson, and Bertram Raphael, A* is particularly suited for pathfinding in video games and robotics.
How It Works
A* uses a best-first search approach by combining the costs to reach the node (g(n)) and a heuristic estimate of the cost to reach the goal from the node (h(n)). The function guides the search, where n is a node on the path. The heuristic h(n) typically satisfies the condition of being admissible, meaning it never overestimates the cost to reach the goal.
Key Characteristics
- Complexity: Similar to Dijkstra's algorithm, the time complexity is in the best case when using a priority queue.
- Optimality: Provides the shortest path when using an admissible heuristic.
- Heuristics: The effectiveness highly depends on the choice of heuristic; good heuristics can significantly reduce the search space.
- Application: Commonly used in games and navigation systems where pathfinding is essential.
Comparison of Dijkstra's Algorithm and A*
The following table summarizes the key differences and similarities between the two algorithms:
| Feature | Dijkstra's Algorithm | A* Algorithm |
| Search Method | Uniform-cost search | Best-first search |
| Heuristic Component | None | Uses heuristics (estimate to goal) |
| Time Complexity | using a priority queue | with a priority queue |
| Admissibility | Always finds the shortest path in graphs with non-negative weights | Finds the shortest path if the heuristic is admissible |
| Space Complexity | ||
| Applications | Network routing, mapping | Video games, robotics, AI pathfinding |
| Strengths | Guaranteed shortest path, simple implementation | Fast with good heuristics, flexible |
| Weaknesses | Slower without a priority queue, can't handle negative weights | Effectiveness depends on the heuristic chosen |
When to Use Which Algorithm?
- Dijkstra's Algorithm: If you are dealing with a network where all weights are non-negative, and you need to find the shortest path between vertices without prior domain knowledge to guide the search.
- A Algorithm*: If your graph represents a space where you can estimate the distance between points, especially when performance is a consideration (e.g., real-time applications). A proper heuristic can significantly speed up A*.
In conclusion, both Dijkstra's Algorithm and A* have their places in the toolkit of a computer scientist or engineer. Dijkstra's straightforward approach offers robust solutions to various pathfinding and network problems, while A*'s heuristic-informed searches excel in contexts where performance needs a boost. Ultimately, the choice between the two depends on the specific requirements and constraints of the problem at hand.
Related reading
- How does Dijkstra's self-stabilizing algorithm work?
- How does Elasticsearch recover from a quorum that is not unanimous
- How does finding a cycle start node in a cycle linked list work?
- How does Firefox's 'awesome' bar match strings?
- How does heap compaction work quickly?
- How does Java implement hash tables?
- How does Google recognizes adult content with safesearch?
- How does Hibernate's batch-fetching algorithm work?

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.