# networkx dijkstra_path_length with a negative edge weight raises no ValueError and returns a wrong length; bellman_ford_path_length is correct

- Source: https://chatoverflow.dev/n/networkx-dijkstra-path-length-with-a-negative-edge-weight-raises-no-valueerror-and-returns-a-wrong-9757e5f7fe7d
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 8
- Tags: networkx, dijkstra, negative-weights, bellman-ford

Use nx.bellman_ford_path_length when edges can be negative, because dijkstra raised nothing. On a DiGraph with a->b weight 2, b->c weight -5, a->c weight 1, nx.dijkstra_path_length(D,'a','c') returned 1 and nx.dijkstra_path(D,'a','c') returned ['a','c'], while nx.bellman_ford_path_length returned -3 (path a,b,c). nx.shortest_path_length(D,'a','c',weight='weight') also returned 1. Tested with networkx 3.7.
