Repository navigation
Conversation
By defaut we continue using the Ford-Fulkerson algorithm. Tests show that it's faster than the recently added preflow push algorithm. Also some cosmetic code and documentation changes.
|
If I understand it correctly, the existing code for connectivity handles unweighted problems. Then you are dealing with unit-capacity networks, for which the shortest augmenting path method is the correct choice. Sub-O(nm) running times have been proved for several such algorithms/implementations (but not for the current implementation of |
|
Specifically to the code, I believe that |
Yes that's right. By the way, a great thing about your PR #1094 is that it handles (only positive) weighted graphs.
Oh I see. Now I understand. Thanks for the nice references. If someone is interested, here are the URLs for pdf versions of the papers. http://www.cs.princeton.edu/courses/archive/fall03/cs528/handouts/network%20flow%20and%20testing.pdf |
Thinking a bit more about this, I'm now convinced that it's better to also add a parameter for the local maximum flow function to all cut functions, so we can use any function in the future (it would be great to have an implementation of Dinic's algorithm) even if right now the only alternative is I'm working on this but I find that the flow functions are not very handy for the needs of the cut functions. I think I'll try to refactor them a bit. Will update this PR with the work in progress. |
|
To me, it makes little sense to use preflow-push algorithms in this scenario. For unweighted problems, Ahuja and Orlin's shortest augmenting path algorithm is probably the algorithm of choice. It is more or less a mix of the Dinic and preflow-push algorithms, approximating the former's layered networks with the latter's distance labels. It has the same asymptotic running time as the former while heuristics speeding up the latter also apply. Because it reduces the time to discover an augmenting path from O(m + n) to O(n), it should almost always outperform the Edmonds–Karp algorithm in |
This is a first try on how to refactor the interface to flow algorithms for playing nice with the flow based connectivity and cut algorithms. The changes are: 1. Each maximum flow implementation is now in its own file in the flow directory. Each implementation provides at least 3 functions: `name_impl_value`, `name_impl_flow` and `name_impl_residual`. They return the value of the maximum flow, the flow dict, and the residual network. For backwards compatibility ther is a function `name_impl` that returns a tuple with the flow value and the flow dict. 2. The functions max_flow and min_cut accept now a parameter for the maximum flow value function to use, with a global default configuration. This is the same approach taken in networkx#1095 for connectivity and cuts functions. I think that having access to the residual network can be useful in general and not only for internal use, at least for a fraction of NetworkX users. Maybe we should set up the flow package as the bipartite package, in which only a few functions are imported in the global namespace and if the user wants to access to all its functionality they have to explicitly import it. See networkx#1096 and networkx#1095 for discussion.
Also applies cut algorithms tests to all maximum flow implementations available (ford_fulkerson and preflow_push).
Yes, it would be very nice to have an implementation of this algorithm. With this changes all connectivity and cut functions accept a parameter for the maximum flow implementation to use, so it will be easy to change the default algorithm. Regarding the use of preflow-push algorithms in this scenario, I agree that makes little sense for actual applications, but it useful for tests. Now all connectivity and cut tests use all maximum flow implementations available. This PR is now a bit too messy because I've also added the commit for refactoring the interface to flow algorithms. I can do 2 separate new PR if people agree that this approach is useful. |
|
For connectivity problems, there needs to be a mechanism to limit the number of augmentations in the maximum flow algorithm. When it is known that the current flow problem will not improve the solution, there is no need to run the algorithm to the end. |
I'm not sure I follow you. Do you mean like adding a cutoff parameter to the flow algorithm to stop augmentations as soon as the the current flow is |
|
When you solve a sequence of min-cut problems P1, P2, P3, P4, and take the minimum of the them, once you know that the minimum of P1, P2, P3 is 10, there is no need to run P4 for more than 10 augmentations. |
Oh I see. Very good point. I think that it will also help to speed up connectivity functions to consider the degree of s and t, because both edge and node connectivity are bounded by degree (ie |
|
I will take a look at adding
|
This refactoring follows from the discussion at networkx#1096 and networkx#1095. It is fully backwards compatible. The main changes are: 1. Rename max_flow and min_cut to maximum_flow and minimum_cut to be consistent with NetworkX naming convention. The short names can still be used (eg nx.max_flow(...)) but are not in the documentation. 2. maximum_flow and minimum_cut accept a new parameter flow_func for the function that will perform the computation. A default_flow_func is set, which is preflow_push_value, based on the benckmarks by @ysitu at networkx#1099. 3. The legacy implementation of ford_fulkerson and firends is separated in its own file and refactored for it to follow the new conventions in the function names, the implementation is the same. 4. Added a new function that return the residual network for each algorithm implementation. Also added docstrings explaining the conventions used in the residual network. 4. Refactored tests. Now it is easy to add a new maximum flow algorithm for testing. They take maybe a bit too long, but now we test all flow related functions. 5. The functions <algo_name>_{value|flow|residual} are not imported to NetworkX base namespace, it is necessary now to explicitly import them from networkx.algorithms.flow, the exception is ford_fulkerson_flow for backwards compatibility. 6. The maximum flow related functions imported in networkx namespace are: maximum_flow, minimum_cut, ford_fulkerson, preflow_push, shortest_augmenting_path. For convenience max_flow and min_cut are also imported. And for backwards compatibility: ford_fulkerson_flow and ford_fulkerson_flow_and_auxiliary.
|
That's great! Adding a cutoff parameter will allow to speed up connectivity and cut functions (significantly, I hope). It's clear that this parameter does not make sense for all algorithms. I'm not sure if it is better to ignore it or to raise an exception then. Internally (for tests and connectivity) it is more comfortable that all functions accept it and, if not applicable, just ignore it. But perhaps is better to be correct here and raise an exception if it does not make sense. And finally, I agree that it is better to keep |
|
I think that I will put off adding "cutoff" until #1102 is resolved. |
This refactoring follows from the discussion at networkx#1096 and networkx#1095. It is fully backwards compatible. The main changes are: 1. Rename max_flow and min_cut to maximum_flow and minimum_cut to be consistent with NetworkX naming convention. The short names can still be used (eg nx.max_flow(...)) but are not in the documentation. 2. maximum_flow and minimum_cut accept a new parameter flow_func for the function that will perform the computation. A default_flow_func is set, which is preflow_push_value, based on the benckmarks by @ysitu at networkx#1099. 3. The legacy implementation of ford_fulkerson and firends is separated in its own file and refactored for it to follow the new conventions in the function names, the implementation is the same. 4. Added a new function that return the residual network for each algorithm implementation. Also added docstrings explaining the conventions used in the residual network. 4. Refactored tests. Now it is easy to add a new maximum flow algorithm for testing. They take maybe a bit too long, but now we test all flow related functions. 5. The functions <algo_name>_{value|flow|residual} are not imported to NetworkX base namespace, it is necessary now to explicitly import them from networkx.algorithms.flow, the exception is ford_fulkerson_flow for backwards compatibility. 6. The maximum flow related functions imported in networkx namespace are: maximum_flow, minimum_cut, ford_fulkerson, preflow_push, shortest_augmenting_path. For convenience max_flow and min_cut are also imported. And for backwards compatibility: ford_fulkerson_flow and ford_fulkerson_flow_and_auxiliary.
|
Now that |
|
I agree, this PR is obsolete now. I plan to work on the refactoring of the connectivity package this weekend (in addition to use SAP with |
Modify all node and edge connectivity functions to accept an additional parameter for defining the implementation in the computation of local maximum flow. With the recent addition of the
preflow_pushalgorithm by @ysitu, we now have two maximum flow implementations.I was going to set the new
preflow_pushas the default function, but it seems that in connectivity algorithms it is actually slower than @loicseguin implementation offord_fulkersonalgorithm. I've put together a small benchmark for maximum flow implementations for connectivity at:https://gist.github.com/jtorrents/9992146
You need the changes in this PR to run the benchmark. In my machine (linux 64-bit) the results using python 2.7 are:
As you can see,
preflow_pushis quite slower thanford_fulkersonfor the kind of computations needed for connectivity algorithms (ie multiple max flow computations on arbitrary pairs of non-connected nodes). However, @ysitu in #1082 showed in a different kind of benchmark thatpreflow_pushoutperformedford_fulkerson. Not sure why is that. Any ideas?Note that the difference in time is bigger in
node_connectivitycomputations. Maybe the structure of the auxiliary digraph used in connectivity algorithms has something to do withford_fulkersonoutperformingpreflow_push?I did not modify analogously the cut functions because the residual graph returned by
preflow_pushneeds preprocessing (remove all edges that exhausted their capacity), thus increasing even more the time required for cuts using thepreflow_pushalgorithm. However if someone thinks it would be useful, I can do the necessary changes.