How this calculation works
A weighted shortest route minimizes the sum of edge weights while respecting direction. Bellman–Ford allows negative weights, unlike algorithms that assume every edge is nonnegative. This page returns one finite path when a minimum exists, identifies unreachable targets and distinguishes a target whose value is unbounded below because a reachable negative cycle can also reach it. Edge-row identities remain visible so parallel edges with different weights cannot be confused.
Relax d(v) to d(u)+w(u,v) while smaller; after |V|−1 rounds, propagate vertices affected by a source-reachable negative cycle.A worked example
For directed edges 1→2 of weight two, 2→3 of weight minus one and 1→3 of weight five, the minimum route from one to three is 1→2→3 with total one. Choosing the fewest edges would instead take the direct edge and return five, which solves an unweighted-hop problem. A negative edge does not itself invalidate a directed calculation; the decisive obstruction is a usable negative cycle on a walk toward the target.
Good to know
Supports 1–40 labeled vertices, at most 200 edge rows and whole weights within ±10⁶. Directed and undirected modes have explicit traversal rules. Parallel edges and self-loops retain their entered identities. The optimization is over weighted walks, which makes relevant negative cycles unbounded; it does not solve a shortest simple path constrained to avoid repeated vertices. A tie can have several minimum routes, and only one is returned. Input weights are abstract costs, not automatically geographic distances.
Reference: NetworkX — shortest paths and negative-weight behavior (opens in a new tab) ↗
A reachable negative cycle matters to the target only if the target is reachable from it
Suppose the source can enter a cycle of total weight minus one. Repeating it makes a walk cheaper on every lap. If there is then a directed continuation to the target, no finite route is minimal. If the cycle lies on a branch from which the target cannot be reached, the target can still have a finite best route. The page reports source-reachable negative cycles separately from the target classification, instead of discarding every useful distance whenever any such cycle exists.
An unreachable target is different again: there is no directed walk from the source, so a large positive placeholder is not a meaningful result. The source-to-source value is zero through the empty walk unless a relevant negative cycle can return to the source. A negative self-loop is such a cycle. These cases are checked through reachability and propagation, rather than by inspecting only the sign of the final displayed edge or assuming source and target must differ.
The Maximum flow minimum cut calculator for a directed graph treats edge values as capacities rather than additive path costs.
Changing the orientation can change whether a finite minimum exists
In an undirected graph, one negative edge can be traversed forward and backward repeatedly, creating a negative closed walk. The same endpoint pair in directed mode may provide no route back and therefore no negative cycle. Switching the form’s orientation changes the mathematical graph; it is not merely a cosmetic choice for drawing arrowheads. The output table always names the actual traversal direction and its original input row.
Integer weights keep the bounded arithmetic exact here. The returned finite path is checked by summing its chosen edge weights and comparing with the computed distance. A negative-cycle witness is also checked to have negative total weight. Those certificates do not impose a simple-path restriction: a user who forbids revisiting vertices is asking a different optimization problem. Likewise, equal-cost alternatives can produce different but valid paths, so a cross-check should compare feasibility and cost before requiring an identical route.
A few questions, answered
Why can a negative undirected edge give no finite minimum?
Both traversal directions are permitted, so a walk can go out and back along that edge repeatedly. Its cost decreases without bound when the cycle can connect the source to the target.
Can the graph include vertices with no edge rows?
Yes. The declared vertex count includes isolates, and empty edge input is allowed. Their reachability is handled explicitly rather than inferred only from endpoint appearances.