How to use this calculator
Find a least-weight directed path, including graphs with negative edges but no reachable negative cycles.
- Enter Node count, Edges as from to weight, comma separated, Source node, Target node. Match the units shown beside each field and review the result together with the assumptions below.
- Read the worked example below to check the expected input format and result.
- Compare another scenario by changing one input at a time. Use Reset to return to the starting values.
Weighted directed shortest path explained
The shortest directed route minimizes the sum of edge weights, rather than the number of edges. Bellman–Ford relaxation permits negative edges, but a reachable negative cycle makes the repeated-walk cost unbounded and is rejected by this implementation.
Inputs, symbols, and units
n— Node count- Whole node count from 2 through 100; identifiers run from 1 to this count.
e— Edges as from to weight, comma separated- Directed edges as source node, destination node and signed weight; separate edge triples with commas, up to 500 edges.
s— Source node- Starting node identifier in the declared graph.
t— Target node- Destination identifier; edge direction is respected.
Formula and method
Bellman–Ford relaxation with reachable negative-cycle detection
Initialize source distance to zero and other distances to infinity. Relax every directed edge up to n−1 times, then inspect for a further improvement from a reachable node. Predecessors reconstruct one least-weight path when no reachable negative cycle prevents the result.
A worked scenario
Node count = 4; Edges as from to weight, comma separated = 1 2 3, 1 3 1, 3 2 1, 2 4 2, 3 4 8; Source node = 1; Target node = 4.
- Minimum path weight: 4 weight units
- Node sequence: 1 → 3 → 2 → 4
Route 1→2→4 has weight 5 and route 1→3→4 has weight 9. The winning route 1→3→2→4 has weight 1+1+2=4. The output reports the node sequence as well as the minimum cost.
Assumptions and edge cases
Up to 100 nodes and 500 edges. Reachable negative cycles are rejected; no path is reported when the target is unreachable.
Frequently asked questions
Can weights be negative?
Yes, if no negative cycle is reachable from the source.
Are edge directions respected?
Yes; add the reverse edge for two-way travel.
Are node labels one based?
Yes.
How do I use the Weighted directed shortest path calculator?
Enter Node count, Edges as from to weight, comma separated, Source node, Target node. Match the units shown beside each field and review the result together with the assumptions below.
What is a worked example of Weighted directed shortest path?
Node count = 4; Edges as from to weight, comma separated = 1 2 3, 1 3 1, 3 2 1, 2 4 2, 3 4 8; Source node = 1; Target node = 4. Minimum path weight: 4 weight units Node sequence: 1 → 3 → 2 → 4 Route 1→2→4 has weight 5 and route 1→3→4 has weight 9. The winning route 1→3→2→4 has weight 1+1+2=4. The output reports the node sequence as well as the minimum cost.
Method reference: NetworkX graph algorithm definitions.
Read our calculation methodology.