Repository navigation
Refactor flow package. - #1102
Conversation
There was a problem hiding this comment.
I wonder if it would be okay to remove the backwards compatibility imports. It's a very simple change for people to make in their code, should they choose to upgrade to the latest NetworkX---which I'm guessing could be 2.0.
|
@jtorrents I am thinking that perhaps I will not make a separate PR for my |
There was a problem hiding this comment.
The compute_flow parameter of preflow_push_impl should be exposed. Some problems (e.g., the closure problem) need only maximum preflows.
Docstring for compute_flow:
compute_flow : bool
If True, compute a maximum flow; otherwise, compute a maximum preflow. Default
value: True.
@ysitu, that's true. I'm thinking that maybe we can use the |
@chebee7i I also think that we could be a bit flexible with backwards compatibility here. Perhaps we can remove definitively |
|
Thanks for bringing in my commits. I have pushed two more follow-up commits. Please also bring them in. I think that the public API can be made more compact.
|
There was a problem hiding this comment.
"(default 'capacity')" is not needed.
Done! I hope that the history of this PR is not too messy for merging it. What do you think @chebee7i?
That's interesting. I also have the feeling that having separated Regarding of what should This is not documented, and the docstrings refer only to the flow_value. Moreover, it might be a bit confusing for the users, especially if they use Regarding the options, I think that is better to change the name to The more I think about it, the more I like your proposal, but it might have to wait until 2.0 if we are to maintain backwards compatibility with how |
|
Regarding the use of for loops in maximum flow tests, the last commit adds a custom message to |
|
Can if flow_func is nx.ford_fulkerson:
return ford_fulkerson_specific_code()This is will make removing it very easy in 2.0. |
|
On second thought, maybe it is better that the old |
|
Regarding a better name from The precise semantics of I believe that implementations of the algorithms do not need the Regarding returning the flow/cut value vs the flow/cut itself from |
|
To the issue of messiness: design is hard, and redesign is very rarely a pretty process. So the messiness of the PR isn't an issue to me. There have been far simpler pull requests (some by me) which have been hard to follow as well. :) I think the main focus here should be on good design, with backwards compatibility issues being an after-thought. That's not to say that backwards compatibility isn't important...it just tends to influence the design. Once a happy design is found, we can always create a separate pull request which adds a compatibility layer back in. |
This implements most of ysitu's proposal (see networkx#1102) on the inetrfaces of flow algorithms. It is still backwards compatible. The main changes are: 1. Removed all {residual|value|flow} functions for the new algorithms. Each algorithm exposes only a function named `algorithm_name` that returns the residual network after computing the maximum flow. Internally we use residual networks everywhere. This simplifies code and tests. 2. Added a new parameter `legacy` to `ford_fulkerson` (default True). If it's True it returns a tuple (flow_value, flow_dict) as it used to do. If `legacy` is False then it returns the residual network, with the flow_value and the flow_dict as graph attributes. Thus we can use it internally (tests, other functions) in more or less the same way that we use the new algorithms. Expanded docstrings explaining all this. The functions `ford_fulkerson_value` and `ford_fulkerson_residual` introduced in the first refactoring are removed. The functions `ford_fulkerson_flow` and `ford_fulkerson_flow_and_auxiliary` are kept here for backwards compatibility, but I'll remove them in a separated commit. 3. I named the parameter `value_only` (ysitu proposed `min_cut_only`) and changed a bit the semantics. It defines what the functions `maximum_flow` and `minimum_cut` return. If True they return the maximum flow value, otherwise they return the actual flow or the set of edges that form the minimum cut. All algorithms recognize this parameter, for the ones in which it's not applicable they just ignore it. 4. The `maximum_flow` and `minimum_cut` functions deal now with `ford_fulkerson` as especial case. They also accept `**kwargs` and pass them to the function implementing the computation. This still needs work and do some checks on the sanity of the arguments passed to flow algorothms. 5. We import everything again from the flow package, without the {residual|value|flow} functions there is no need to keep stuff only to the flow package. 6. I've just adapted the old tests, more tests needed for testing the interface and new features of maximum_flow and minimum_cut. Now tests take less time because we are not testing redundant {residual|value|flow} functions. 7. `max_flow` and `min_cut` are alias for `maximum_flow` and `minimum_cut`. I think it's not a good idea to have them behave different, it could be a source of confusion.
|
Sorry for being slow on this, I'm quite busy this days. After playing a bit with ysitu proposal, I think that it is quite nice and the way to go for the interfaces of flow algorithms. The commit above implements most of ysitu's proposal with some changes. It is still backwards compatible. The main changes are:
|
|
If you use |
|
2014-04-18 17:24 GMT+02:00 ysitu [email protected]:
|
|
I think that |
Ok, sounds good. Besides, we already have functions in the I'm working on refactoring a bit the tests and removing |
|
Regarding removing the functions |
Add option to reuse residual networks in maxflow algorithms
Great! I'll write some more tests for the flow package, (
Looking at the change in
Oh I see. Checking every time is quite ugly.
No please! in my benchmarks with connectivity algorithms (still without using |
Since the residual network is the same for every pair of nodes, delegate sanity checks on source and sink to algorithm implementations. This makes build_residual_network easier to use in the connectivity package, and in general.
This solution is not very nice but works.
|
The commits above implement these changes:
|
|
Your trouble with There are probably no good solutions. NetworkX is geared towards static graphs. When your graph is dynamic, even in the simple case where the dynamicity originates from with the graph itself (e.g., node/edge attribute-defined topology), pretty much you need to write special code. |
|
Good points. I guess that we will have to wait for a better/more elegant solution until we have a working implementation of a Graph view that can be used to filter nodes and edges in graph methods. Regarding the last changes that we did for allowing reuse of the residual network, they make a huge difference in connectivity algorithms. The code is simpler by taking advantage of the new interface to flow functions, and it is a lot faster in some scenarios (sparse networks with degree distributions skewed to the right, the kind of networks in which I'm interested). I did some benchmarks for node and edge connectivity comparing all new algorithms and the legacy implementation. In this benchmarks, This benchmarks use python 2.7 in a Linux 64-bit system. pypy cuts the time by half approximately: |
This is still work in progress. Tests updated only for connectivity and all docstrings are the old ones. As a starting point, I've kept the flow_func parameter (is very nice for tests) and set edmonds_karp by default. However shortest_augmenting_path is faster in denser networks (this is a reason for keeping the flow_func parameter in connectivity functions). We can discuss more about the inetrface to connectivity algorithms when networkx#1102 is merged and I can do a proper PR. I've post some benchmarks in networkx#1102, and for some problems that I care about I'm seeing one order of magnitude faster runtimes. Great work @ysitu!
The test for cutoff was not picking it. I've added another.
|
In 6fa58b9 I've made With this I think this PR is almost ready for merging (do you agree @ysitu?). In ec56e7d I started the refactoring of the connectivity package using the new maximum flow interfaces and algorithms. I think that the code is clearer, and it is for sure a lot faster than the legacy implementation. For some networks that I care about, I'm seeing one order of magnitude difference in speed. Thanks a lot @ysitu for your work on this! When this is merged I'll do a proper PR from ec56e7d and we can discuss the interface to connectivity algorithms. I'd like to keep the |
|
It was a blunder of mine that I forgot to use |
|
Hmm, it seems that github ate a comment that I did last night. Anyway it was not important, I was just saying that I did the change in handling I keep working on the refactoring of the connectivity package to use the new flow interfaces and algorithms. I'm working on a branch of my fork refactor-conn . So far I have code and tests in good shape, still working on the documentation. However I have some doubts about the interface, about how much to document (eg reuse of residual network and auxiliary digraph, ...) and would like to discuss them in a proper PR, but I'm not sure which is the best course of action:
Which do you think is the best approach? |
|
I think this PR should be merged before it grows so large that it eats all of the other PRs... |
|
This should be merged ASAP. |
|
The most likely situation, in my mind, is that this won't get many more eyes on it...certainly not at the level of detail that has already been put in. Are you guys (@jtorrents and @ysitu) at the point where this is ready? If so, you have my vote to merge it. [If it passes a smell test and all the unit tests continue to pass, I lean for "merge". :) ] |
|
So ends the most-involved PR evaarrrrr.... |
|
Yay! This has been a lot of fun! I guess that we'll see some complains for breaking backwards compatibility, but I'm sure that people will understand when they play a bit with the new interface, and we can also show them how fast are the new algorithms in some scenarios. I'll try to put together another PR with module level documentation showing the performance of flow algorithms in different contexts (we can use the benchmarks provided by @ysitu at #1099), so users can pick the one that fits their problem. I'm preparing the pull request for the refactor of connectivity, but I'm having some problems with git. By the way, @hagberg when do you think the 1.9 release can be ready? I'd like to also include the connectivity improvements. |
Use the new algorithms and the new interfaces provided by the flow package. The code is quite clearer, and the new algorithms and interface provide a significant speed up in running time. For some problems, this code is one order of magnitude faster (see networkx#1102 for bechmarks). So far the code is still backwards compatible, but we should discuss some possible non backward compatible improvements.
minimum_cut has documented the partition as the set of nodes reachable from
the source in the residual network, but has computed the other side instead:
the nodes that can reach the sink, and then the complement. The two agree only
when the sink side is exactly the sink-reachable set, and they disagree
whenever a minimum cut is not adjacent to the sink. On the digraph
35->36->37->39->40->41->46->47, capacities 2,2,2,2,1,1,1
the bottleneck is (40, 41), yet minimum_cut returns
(1, ({35, 36, 37, 39, 40, 41, 46}, {47}))
which is a valid minimum cut -- cutting the saturated edge (46, 47) also
costs 1 -- but reports a partition unrelated to the cut. Randomized testing
over small capacitated digraphs and graphs puts the rate at which that
trivial partition is returned at around 44%.
This is the behaviour reported in networkx#5239, open since 2021.
The sink-side formulation dates back to networkx#1102; networkx#8756 reimplemented it as a
reverse breadth-first search and left the Notes describing the search
direction as running "to t", so the code and its documentation disagreed
throughout. The cut *value* has always been correct, which is why no test
caught this: what is wrong is the partition, so callers asking which nodes or
edges form the bottleneck get an answer unrelated to it.
Search forward from the source instead, over the same flow < capacity
predicate, which is what the residual network Notes describe ("Reachability
from s ... induces a minimum s-t cut"). That needs a residual network holding
a maximum *flow*: a maximum *preflow* (value_only=True) may still leave excess
on intermediate nodes, and the spare-capacity edges leaving such a node would
report a cut that is not minimum. minimum_cut therefore lets the flow
computation run to completion. On random capacitated graphs that measured
between neutral and roughly 1.25x slower for a single minimum_cut call, and
within noise for gomory_hu_tree, which makes n-1 such calls against a reused
residual.
The five residual-network Notes that described the search direction as running
"to t" -- four in maxflow.py, one in preflow_push -- are corrected to match.
They cannot be corrected on their own: at the parent commit the code, its
inline comment and the Notes all describe the sink-side search consistently,
so the wording only becomes true once the search runs from the source.
minimum_cut has documented the partition as the set of nodes reachable from
the source in the residual network, but has computed the other side instead:
the nodes that can reach the sink, and then the complement. The two agree only
when the sink side is exactly the sink-reachable set, and they disagree
whenever a minimum cut is not adjacent to the sink. On the digraph
35->36->37->39->40->41->46->47, capacities 2,2,2,2,1,1,1
the bottleneck is (40, 41), yet minimum_cut returns
(1, ({35, 36, 37, 39, 40, 41, 46}, {47}))
which is a valid minimum cut -- cutting the saturated edge (46, 47) also
costs 1 -- but reports a partition unrelated to the cut. Randomized testing
over small capacitated digraphs and graphs puts the rate at which that
trivial partition is returned at around 44%.
This is the behaviour reported in networkx#5239, open since 2021.
The sink-side formulation dates back to networkx#1102; networkx#8756 reimplemented it as a
reverse breadth-first search and left the Notes describing the search
direction as running "to t", so the code and its documentation disagreed
throughout. The cut *value* has always been correct, which is why no test
caught this: what is wrong is the partition, so callers asking which nodes or
edges form the bottleneck get an answer unrelated to it.
Search forward from the source instead, over the same flow < capacity
predicate, which is what the residual network Notes describe ("Reachability
from s ... induces a minimum s-t cut"). That needs a residual network holding
a maximum *flow*: a maximum *preflow* (value_only=True) may still leave excess
on intermediate nodes, and the spare-capacity edges leaving such a node would
report a cut that is not minimum. minimum_cut therefore lets the flow
computation run to completion. On random capacitated graphs that measured
between neutral and roughly 1.25x slower for a single minimum_cut call, and
within noise for gomory_hu_tree, which makes n-1 such calls against a reused
residual.
The five residual-network Notes that described the search direction as running
"to t" -- four in maxflow.py, one in preflow_push -- are corrected to match.
They cannot be corrected on their own: at the parent commit the code, its
inline comment and the Notes all describe the sink-side search consistently,
so the wording only becomes true once the search runs from the source.
This refactoring follows from the discussion at #1096 and #1095. It is fully backwards compatible. The main changes are: