Skip to content

Fix minimum_cut partition errors with floating-point capacities - #8316

Closed
MusserO wants to merge 2 commits into
networkx:mainfrom
MusserO:bugfix-mincut-float-tolerance
Closed

MusserO wants to merge 2 commits into
networkx:mainfrom
MusserO:bugfix-mincut-float-tolerance

Conversation

@MusserO

@MusserO MusserO commented Oct 9, 2025

Copy link
Copy Markdown

Fixes #4972.
Fixes #4592.

When using floating-point capacities, the partition returned by minimum_cut may be incorrect due to floating-point precision errors.

Example:

import networkx as nx
w = 0.1
G = nx.DiGraph()
G.add_edge("a", "b", capacity=1)
G.add_edge("b", "a", capacity=w)
G.add_edge("a", "c", capacity=2)
G.add_edge("s", "a", capacity=2)
G.add_edge("s", "b", capacity=2)
G.add_edge("a", "t", capacity=1)
G.add_edge("b", "t", capacity=1)
G.add_edge("c", "t", capacity=2)
print(nx.minimum_cut(G, "s", "t"))

On this graph, nx.minimum_cut(G, "s", "t") returns (3.1, ({'s'}, {'a', 'b', 'c', 't'})), but the cost of this returned partition is 4 and the correct return value should be (3.1, ({'b', 's'}, {'a', 'c', 't'})).

I have resolved this issue by replacing the strict equality check with one that can handle floating-point rounding errors.

I have also included this example graph as a test instance and added a small tolerance for the tests to avoid errors when using floating-point capacities.

@dschult

dschult commented Oct 9, 2025

Copy link
Copy Markdown
Member

This is a consistent issue when people use floating point edge/node attributes in algorithms that look for min/max/equality/etc. That is, any time we use floating point data for algorithms where floating point errors change the answers. Our advice is to convert the data to integer type, e.g. int(weight * 1e12), run with that and convert any final result back to floating point. The algorithms just don't work for floating point.

In your example the approximation using those tolerance levels with those data values does work. But those tolerances won't work with other data values. I don't know of a way that we can make this work for any user input. So, our current policy is to make the user decide how to convert to and from floating point, rather than doing the conversion in a way that we choose. We also considered raising an error for floating point values on attributes, but we decided not to take that approach.

It's also possible that its time to re-consider that discussion/decision. But I'm pretty sure we don't want to use approximate computation with tolerance levels that are chosen arbitrarily when they should be chosen based on the data being used.
Thanks for this PR.

Perhaps it would be helpful if you explained what happened that led to this PR. ??

@rossbar rossbar added the floating-point Issues that ultimate stem from numerical precision label Oct 9, 2025
@MusserO

MusserO commented Oct 10, 2025

Copy link
Copy Markdown
Author

Thanks for the comments.

Context:
I noticed my min-cut-based algorithm was returning suboptimal answers, and it took a while to trace it back to this issue of incorrect partitions when using floating-point capacities.

I feel this should be addressed since the documentation for minimum_cut does not include any information about issues with floating-point capacities, and many others have made the same mistake of not converting them into integers. And it is not necessary to require integer capacities since everything works if the floating-point numbers are handled correctly.

If the user intends to use capacities with more than the fixed 1e-12 precision, then this fix indeed does not work, but this could be considered as a rare use case, and it may be sufficient to include the information about floating-point rounding in the documentation. If we want to allow arbitrary tolerance levels, we could also change the API for minimum_cut and set the tolerances as optional parameters that the user can adjust, like in nx.algorithms.isomorphism.matchhelpers

@amcandio

Copy link
Copy Markdown
Contributor

Many algorithms don't work with floating points. The moment you use them here your solution becomes an approximation. I'm +1 on Dan's comment on multiplying by a big N and converting to int.

I see how this can be a common pain point for users. However the problem comes from using floating points and expecting exact results.

Many algorithms will have the same issue, for example Dijkstra's, so I don't think we should update all numeric comparisons in NetworkX to allow for a tolerance.

What we could do here is provide more flexibility for the capacity and allow users to specify a function (like we do for Dijkstra's and cost). This way users don't need to modify the whole input graph. Moreover, we could also update the tutorial with considerations on using floating point as algorithm inputs.

@MusserO

MusserO commented Oct 19, 2025

Copy link
Copy Markdown
Author

You're right, sometimes the algorithms just don't work correctly with floating-point capacities. In the following example, edmonds_karp works correctly, but preflow_push results in an error:

import networkx as nx
from networkx.algorithms.flow import preflow_push, edmonds_karp

G = nx.DiGraph()
G.add_edge("s", "a", capacity=1)
G.add_edge("a", "b", capacity=1)
G.add_edge("b", "c", capacity=1)                                                    
G.add_edge("c", "t", capacity=0.1)
G.add_edge("s", "d", capacity=0.1)
G.add_edge("b", "d", capacity=0.2)

print("edmonds_karp flow:", nx.maximum_flow(G, "s", "t", flow_func=edmonds_karp)[0])  # 0.1

# Raises: ValueError: min() iterable argument is empty (in preflow_push relabel)
print("preflow_push flow:", nx.maximum_flow(G, "s", "t", flow_func=preflow_push)[0])

Allowing floating-point capacities could be done by adding small tolerances to all capacity comparisons in the flow algorithms, but converting the capacities to integers may be a cleaner approach.

@MusserO

MusserO commented Oct 20, 2025

Copy link
Copy Markdown
Author

I will close this and create a new pull request that uses the discussed approach of converting edge capacities to integers instead of adding tolerances to floating-point comparisons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

floating-point Issues that ultimate stem from numerical precision type: Enhancements

Development

Successfully merging this pull request may close these issues.

minimum_cut returns correct value (flow) but wrong partition for some graphs Issue in minimum_cut

4 participants