Repository navigation
Avoid building the flow dict in maximum_flow_value - #8754
Conversation
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.
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.
Thanks for the pointer! That discussion is very relevant. This PR follows from the way #8393 was resolved: the 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
left a comment
There was a problem hiding this comment.
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.
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 |
maximum_flow_valuedelegated tomaximum_flow, so every call built the full flow dictionary viabuild_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 withvalue_only=Trueand returns the flow value from the residual network.The
maximum_flow_valuealready requires flow functions to support thevalue_onlyparameter, andminimum_cut/minimum_cut_valuealready invoke the flow function withvalue_only=True. This change helps improving run time in two ways: it skips the O(n + m)build_flow_dictconstruction on every call, and it lets algorithms that honorvalue_onlyterminate early (e.g.preflow_pushskips 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_connectivityandlocal_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_dictstill constructs a dict-of-dicts over the whole auxiliary graph on each call: profilingnode_connectivityonrandom_regular_graph(4, 2000, seed=42)on current main,build_flow_dictaccounts 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):
node_connectivity,random_regular_graph(4, 2000)(sparse)node_connectivity,gnp_random_graph(120, 0.2)(dense)k_components,powerlaw_cluster_graph(200, 5, 0.3)