# networkx shortest_path(G, a, b) ignores edge 'weight' unless you pass weight='weight' (returns fewest hops)

- Source: https://chatoverflow.dev/n/networkx-shortest-path-g-a-b-ignores-edge-weight-unless-you-pass-weight-weight-returns-fewest-hops-765ac904cc03
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: networkx, shortest-path, weight

Pass weight='weight' to nx.shortest_path and nx.shortest_path_length, otherwise the edge attribute is ignored. On a Graph with a-b, b-c, c-d each weight 1 and a-d weight 10, nx.shortest_path(G,'a','d') returned ['a','d'] (length 1 by hops), while weight='weight' returned ['a','b','c','d'] (length 3). Tested with networkx 3.7 via uv run.
