Does A work with negative weights as long that the heuristic is admissible?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
A* (A-star) is one of the most popular pathfinding and graph traversal algorithms, widely used in computer science fields like artificial intelligence and robotics. Its effectiveness stems from its use of heuristics to provide optimal solutions in many scenarios. However, the question of whether the A* algorithm can handle graphs with negative edge weights, provided that the heuristic is admissible, is crucial in understanding its limitations and capabilities.
A* Algorithm Overview
A* is a search algorithm that finds the shortest path from a starting node to a goal node. It uses a cost function defined by:
- is the actual cost from the start node to the current node .
- is the heuristic estimate of the cost from to the goal node.
The heuristic must be admissible, meaning it never overestimates the actual cost. This property ensures that A* is both complete (it always finds a solution if one exists) and optimal (the path found is the shortest).
Negative Edge Weights: A*’s Limitations
One of A*'s limitations is related to the presence of negative edge weights in the graph. To understand this, the properties of and the role of negative weights must be discussed.
The Issue with Negative Weights
When an edge in a graph has a negative weight, it reduces the cumulative path cost. If the graph also includes cycles, a negative-weight cycle can be traversed repeatedly, continually reducing the path cost . A*'s standard implementation would not normally account for this, as it assumes non-negative weights which ensures that once a node has been expanded with an optimal cost, it will not need reassessment.
Examples with Negative Weights
Consider a simple graph:
| Node | Connected Nodes | Edge Weights |
| A | B, C | 1, -2 |
| B | D | 6 |
| C | B | 2 |
| D | Goal | 1 |
- From A to C to B is cheaper because of the negative weight.
- An admissible heuristic, , could be as follows:
- , , , ,
If A* uses this heuristic, it will choose the path A → C → B → D → Goal, as it eventually finds a cheaper cost through C due to the negative weight. However, if a cycle or more negative edges present, there may be cases where the algorithm enters an infinite loop or produces suboptimal outcomes.
A* and Bellman-Ford Comparison
To handle graphs with negative weights, Bellman-Ford is a preferred algorithm. It iteratively relaxes edges and handles negative weights, detecting negative-weight cycles. Bellman-Ford is generally slower, with a time complexity of in comparison to A*'s (with a binary heap), yet it guarantees shortest paths even in the presence of negative weights.
Table: Key Differences and Capabilities
| Feature | A* Algorithm | Bellman-Ford Algorithm |
| Heuristic Usage | Yes | No |
| Handles Negative Weights | No | Yes |
| Optimality with Admissible Heuristic | Yes | Not Applicable |
| Handles Negative-Weight Cycles | No | Yes (Detects cycle) |
| Time Complexity | (with heap) | |
| Space Complexity |
Conclusion
In summary, A* requires non-negative weights to ensure it completes and finds an optimal path efficiently. Negative weights introduce complexities that an admissible heuristic alone cannot resolve. The A* algorithm's design does not account for negative-weight cycles or edges in the same way other algorithms like Bellman-Ford do. Hence, in scenarios requiring traversal over graphs with negative weights, it is crucial to either preprocess the graph to eliminate such edges or choose an alternative algorithm designed to handle them.
Related reading
- Does algorithm define a macro X?
- Does an algorithm exist to help detect the primary topic of an English sentence?
- Does an Enum exist for Asc or Desc ordering?
- Does anyone have a good Proper Case algorithm
- Does anybody know how B-Tree got its name?
- Does C have a way of giving me an immutable Dictionary?
- Does Big O Measure Memory Requirments Or Just Speed?
- Does Dijkstra's algorithm apply even if there is only one negative weight edge?

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.