Skip to content

DOC: Add example of floating point handling for maximum_flow capacity - #8761

Merged
dschult merged 3 commits into
networkx:mainfrom
dschult:max_flow_doc_example_floating_point
Jul 17, 2026
Merged

dschult merged 3 commits into
networkx:mainfrom
dschult:max_flow_doc_example_floating_point

Conversation

@dschult

@dschult dschult commented Jul 14, 2026

Copy link
Copy Markdown
Member

This PR adds an example to the doc_string for maximum_flow. The example shows how one can use the capacity kwarg as a function to adjust the weights to integers and eliminate round-off errors.

This is possible because of #8406 which enabled functions in the capacity kwarg.
There are many other functions where this sort of thing could be done. Perhaps long term we want to discuss floating point errors in one place of the docs and point to it from others. That one place could also point to doc_strings for each suite of functions where one example is housed for that suite. For example, this would be the canonical example for flow and cut functions. Those functions would point to the main page which would explain the trouble and solution as well as house a list of pointers to examples, the "flow and cut" example could be this one.

An example like this first appeared in #8402.

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @dschult - one minor comment re: making the roundtripping just a tad more obvious to a reader - feel free to take it or leave it!

Comment thread networkx/algorithms/flow/maxflow.py Outdated
Comment on lines +181 to +184
>>> flow_value, _ = nx.maximum_flow(
... G, 0, 1, capacity=lambda u, v, d: int(1e6 * d["capacity"])
... )
>>> flow_value / 1e6 == 0.9

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion to bind the precision level to a variable to make it (very slightly) more obvious how it is used both on the flow func and in the rescaling of the result!

Suggested change
>>> flow_value, _ = nx.maximum_flow(
... G, 0, 1, capacity=lambda u, v, d: int(1e6 * d["capacity"])
... )
>>> flow_value / 1e6 == 0.9
>>> prec = 1e6
>>> flow_value, _ = nx.maximum_flow(
... G, 0, 1, capacity=lambda u, v, d: int(prec * d["capacity"])
... )
>>> flow_value / prec == 0.9

@dschult
dschult merged commit 4e89e66 into networkx:main Jul 17, 2026
43 checks passed
@dschult
dschult deleted the max_flow_doc_example_floating_point branch July 17, 2026 19:07
@dschult dschult added this to the 3.7 milestone Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants