Skip to content

Avoid building the flow dict in maximum_flow_value - #8754

Merged
dschult merged 3 commits into
networkx:mainfrom
jtorrents:maxflow-value-only
Jul 17, 2026
Merged

dschult merged 3 commits into
networkx:mainfrom
jtorrents:maxflow-value-only

Conversation

@jtorrents

Copy link
Copy Markdown
Member

maximum_flow_value delegated to maximum_flow, so every call built the full flow dictionary via build_flow_dict (a fresh dict-of-dicts over all nodes and edges) and immediately discarded it, keeping only the scalar value. This PR calls the flow function directly with value_only=True and returns the flow value from the residual network.

The maximum_flow_value already requires flow functions to support the value_only parameter, and minimum_cut / minimum_cut_value already invoke the flow function with value_only=True. This change helps improving run time in two ways: it skips the O(n + m) build_flow_dict construction on every call, and it lets algorithms that honor value_only terminate early (e.g. preflow_push skips its second phase, which computes the actual flows).

This change has a significant impact in flow based connectivity: both connectivity primitives return nx.maximum_flow_value(...) directly: local_node_connectivity and local_edge_connectivity; so this benefits everything built on them: node_connectivity, edge_connectivity, all_node_cuts, k_components, and the connectivity approximations.

The size of the gain depends on how expensive each max-flow is relative to the discarded dict. It is largest on sparse graphs, where every flow terminates after at most k augmentations (k small) while build_flow_dict still constructs a dict-of-dicts over the whole auxiliary graph on each call: profiling node_connectivity on random_regular_graph(4, 2000, seed=42) on current main, build_flow_dict accounts for 58% of the running time. On denser graphs the flow computation itself dominates and the gain is smaller but still visible.

Benchmarks (best of 5):

benchmark main this PR
node_connectivity, random_regular_graph(4, 2000) (sparse) 15.94s 6.03s (~2.6x)
node_connectivity, gnp_random_graph(120, 0.2) (dense) 0.365s 0.231s (~1.6x)
k_components, powerlaw_cluster_graph(200, 5, 0.3) 4.220s 3.696s (~1.15x)

maximum_flow_value delegated to maximum_flow, so every call built the
full flow dictionary via build_flow_dict and discarded it. Call the
flow function directly with value_only=True and return the flow value
from the residual network, mirroring minimum_cut; the docstring already
required flow functions to support value_only.

This benefits every local_node_connectivity and local_edge_connectivity
caller: node_connectivity, edge_connectivity, all_node_cuts, the
k-components algorithms, and the connectivity approximations. The gain
is largest on sparse graphs, where each max-flow terminates after a few
augmentations and the O(n + m) dict construction dominates the per-call
cost.
Comment thread networkx/algorithms/flow/maxflow.py Resolved

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

See also #8393

Extend the Raises sections, as suggested in review: maximum_flow and
maximum_flow_value did not document the NetworkXError raised when
flow_func is not callable or when kwargs are passed without explicitly
setting flow_func, and minimum_cut and minimum_cut_value documented no
NetworkXError at all, including the cutoff check specific to them.
Also fix the error type named in the NetworkXUnbounded entry of the
minimum cut functions, and a small typo repeated in the four Notes
sections.
@jtorrents

Copy link
Copy Markdown
Member Author

See also #8393

Thanks for the pointer! That discussion is very relevant. This PR follows from the way #8393 was resolved: the _value functions stay. And it addresses what I think #8392 overlooked when it unified the implementations for maintainability: by delegating, maximum_flow_value unconditionally builds the flow dictionary even though it only needs the value stored in R.graph["flow_value"]. That's harmless for a one-off call, but flow-based connectivity algorithms (node_connectivity, all_node_cuts, k_components, ..) call maximum_flow_value potentially millions of times. As the numbers in the PR description show, build_flow_dict accounts for 58% of the runtime of a sparse node_connectivity computation.

The trade-off is reintroducing some of the duplication #8392 removed, but it's now limited to the small flow_func validation block at the top. The rest of the two bodies do genuinely different work.

@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 see - this makes sense given the existing API! It's too bad that that the underlying computation (i.e. calling the flow function) is obfuscated from the user; but that's another issue :)

I'd advocate for adding a comment to the top of the function which mentions that all flow_func s produce a residual graph with a flow_value graph attr which stores the maximum flow. The additional context will be important for the next time I try to deprecate these functions 🙃

Otherwise this LGTM, thanks @jtorrents for chasing this down!

Add a comment noting that every flow function returns a residual
network that stores the maximum flow value in R.graph["flow_value"],
as requested in review.
@jtorrents

Copy link
Copy Markdown
Member Author

I see - this makes sense given the existing API! It's too bad that that the underlying computation (i.e. calling the flow function) is obfuscated from the user; but that's another issue :)

I'd advocate for adding a comment to the top of the function which mentions that all flow_func s produce a residual graph with a flow_value graph attr which stores the maximum flow. The additional context will be important for the next time I try to deprecate these functions 🙃

Otherwise this LGTM, thanks @jtorrents for chasing this down!

Thanks for the review! Comment added in 2f32769 , right above the flow_func call: every flow function returns a residual network with the maximum flow value stored in R.graph["flow_value"]. Hopefully it saves the next deprecation attempt some archaeology :)

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

Thanks!

@dschult
dschult merged commit 6a09d80 into networkx:main Jul 17, 2026
43 checks passed
@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.

4 participants