A* algorithm
negative weights
admissible heuristic
pathfinding
graph theory

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.

Practice algorithms

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:

latex
f(n) = g(n) + h(n)
  • g(n)g(n) is the actual cost from the start node to the current node nn.
  • h(n)h(n) is the heuristic estimate of the cost from nn to the goal node.

The heuristic h(n)h(n) 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 g(n)g(n) 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 g(n)g(n). 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:

NodeConnected NodesEdge Weights
AB, C1, -2
BD6
CB2
DGoal1
  • From A to C to B is cheaper because of the negative weight.
  • An admissible heuristic, h(n)h(n), could be as follows:
    • h(A)=4h(A) = 4, h(B)=2h(B) = 2, h(C)=3h(C) = 3, h(D)=1h(D) = 1, h(Goal)=0h(\text{Goal}) = 0

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 O(VE)O(V \cdot E) in comparison to A*'s O(E+VlogV)O(E + V \cdot \log V) (with a binary heap), yet it guarantees shortest paths even in the presence of negative weights.

Table: Key Differences and Capabilities

FeatureA* AlgorithmBellman-Ford Algorithm
Heuristic UsageYesNo
Handles Negative WeightsNoYes
Optimality with Admissible HeuristicYesNot Applicable
Handles Negative-Weight CyclesNoYes (Detects cycle)
Time ComplexityO(E+VlogV)O(E + V \cdot \log V) (with heap)O(VE)O(V \cdot E)
Space ComplexityO(V)O(V)O(V)O(V)

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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms