Skip to content

Added floating-point comparison example - #8402

Closed
Jiya873 wants to merge 15 commits into
networkx:mainfrom
Jiya873:doc-floating-point-maximum-flow
Closed

Jiya873 wants to merge 15 commits into
networkx:mainfrom
Jiya873:doc-floating-point-maximum-flow

Conversation

@Jiya873

@Jiya873 Jiya873 commented Dec 18, 2025

Copy link
Copy Markdown
Contributor

fix: #8389

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for this!

Comment thread networkx/algorithms/flow/maxflow.py Resolved
Comment thread networkx/algorithms/flow/maxflow.py Resolved

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

OK Thanks!

Now I'd like to dive into the floating point troubles. With shortest_path, we could end up with incorrect shortest paths -- not just off by a "small" amount, but off by seemingly a lot more than the original round-off error.

Can that happen here? The new example here tells folks how to apply round-off to the final answer. But it doesn't apply round-off to each capacity in the network.

I think what we need is to convert all the capacity values to integers e.g. int(10*value) before calling nx.maximum_flow(G, "s", "t"). Another solution would involve changing maximum_flow to allow a capacity function (like we did with the shortest_path functions weight argument). I believe that is in #8406. So maybe this should wait for that to be merged??

method 1 code:

old_cap = nx.get_edge_attributes(G, "capacity")
nx.set_edge_attributes(G, {e: int(1e6 * c) for e, c in old_cap.items()}, "new_capacity")
flow_value, flow_dict = nx.maximum_flow(G, "s", "t")

method 2 code:

def int_capacity(u, v, eattr):
    return int(1e6 * eattr["capacity"])

flow_value, flow_dict = nx.maximum_flow(G, "s", "t", capacity=int_capacity)

@Ashtam01

Copy link
Copy Markdown

Hi @Jiya873 , Rossbar closed my PR (#8414) to keep the discussion consolidated here. I had already written and tested the examples for minimum_cut and minimum_cut_value using math.isclose as discussed. Since you are working on the flow functions, feel free to use this snippet to cover the cut functions if needed:

    If the capacities are floating point numbers, usage of ``math.isclose``
    is recommended for value comparison to avoid machine-dependent errors:

    >>> import math
    >>> G = nx.DiGraph()
    >>> G.add_edge("x", "y", capacity=0.1)
    >>> G.add_edge("y", "z", capacity=0.1)
    >>> G.add_edge("z", "x", capacity=0.1)
    >>> cut_value, partition = nx.minimum_cut(G, "x", "z")
    >>> math.isclose(cut_value, 0.1)
    True 

@Jiya873

Jiya873 commented Dec 26, 2025

Copy link
Copy Markdown
Contributor Author

@dschult, I agree with your concern about floating-point issues, similar to what can happen with shortest_path and both approaches you suggested make sense to me.
I have also gone through the discussion and commits in #8406, and I agree that it’s best to wait until that PR is merged.
I want to ask should I update the examples for maximum_flow_value, minimum_cut, and minimum_cut_value, or should I wait and handle those after the PR is merged and we handle maximum_flow function first?

@Jiya873

Jiya873 commented Dec 26, 2025

Copy link
Copy Markdown
Contributor Author

Hi @Ashtam01,
thanks for sharing this. I’ll definitely refer to your snippet if we decide to keep a math.isclose based example or need a comparison.

@Jiya873

Jiya873 commented Jan 6, 2026

Copy link
Copy Markdown
Contributor Author

Hi @dschult, since this PR(#8406) is merged I think we can proceed now here.

Comment thread networkx/algorithms/flow/maxflow.py Resolved
@gulshan-123

gulshan-123 commented Jan 9, 2026 •

Copy link
Copy Markdown
Contributor

@Jiya873 IMO the problem here was not merging main with this PR, as I can see now the capacity function works?

Details

Since capacity is constructed by default as infinite capacity if that attribute is not present in edge. And the attribute being passed was a function, thus it was absent from the attribute dict, and hence, error was silent and all capacities get constructed infinite and hence NetworkXUnbounded.

after the main merging in this PR, e1a7a64, the error is no longer there.

Hence, we can remove the newly added test, and add it to the example section as suggested by @dschult .

flow_value, _ = nx.maximum_flow(G, "s", "t", capacity=lambda d: round(1e6 * d["capacity"]))

Also, as it is correct now in latest commit, the capacity function must take three parameters, the first two are the edges u and v, the third is the edge attribute.

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks good. Thanks!

@amcandio amcandio 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.

This PR looks good to me! Although I'm having doubts whether we should really clarify this in all methods. Sounds like too noisy/duplicated.

@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.

I generally agree with @amcandio - this is a lot to add; the example is also incomplete as the resulting flow value is still "transformed", i.e. is not scaled back to the correct capacities.

My personal vote would be to significantly trim this down!

floating point arithmetic.

Examples affecting comparisons
------------------------------

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.

Adding non-numpydoc headings will break docstring rendering, so this should be removed.

@Jiya873 Jiya873 closed this by deleting the head repository Mar 6, 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.

DOC: Add examples of floating point conversion to docstrings

6 participants