Skip to content

Add a parameter in connectivity functions to select a max_flow function - #1095

Closed
jtorrents wants to merge 5 commits into
networkx:masterfrom
jtorrents:flow-cuts
Closed

jtorrents wants to merge 5 commits into
networkx:masterfrom
jtorrents:flow-cuts

Conversation

@jtorrents

Copy link
Copy Markdown
Member

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_push algorithm by @ysitu, we now have two maximum flow implementations.

I was going to set the new preflow_push as the default function, but it seems that in connectivity algorithms it is actually slower than @loicseguin implementation of ford_fulkerson algorithm. 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:

jtorrents@saturn:~/projects/benchmark$ python2.7 benchmark_flow.py
Name: fast_gnp_random_graph(600,0.02)
Type: Graph
Number of nodes: 600
Number of edges: 3633
Average degree:  12.1100
Mean and std of computing the maximum flow among 100 pairs of nodes
  Kind of capacity: unit
    preflow_push:   0.022 (0.0033) seconds
    ford_fulkerson: 0.012 (0.0021) seconds
  Kind of capacity: random ints
    preflow_push:   0.024 (0.0039) seconds
    ford_fulkerson: 0.013 (0.0024) seconds
Time computing node connectivity
    preflow_push:   33.42 seconds
    ford_fulkerson: 9.68 seconds
Time computing edge connectivity
    preflow_push:   3.54 seconds
    ford_fulkerson: 1.69 seconds

Name: fast_gnp_random_graph(200,0.2)
Type: Graph
Number of nodes: 200
Number of edges: 3962
Average degree:  39.6200
Mean and std of computing the maximum flow among 100 pairs of nodes
  Kind of capacity: unit
    preflow_push:   0.020 (0.0031) seconds
    ford_fulkerson: 0.012 (0.0021) seconds
  Kind of capacity: random ints
    preflow_push:   0.021 (0.0033) seconds
    ford_fulkerson: 0.013 (0.0022) seconds
Time computing node connectivity
    preflow_push:   20.70 seconds
    ford_fulkerson: 7.68 seconds
Time computing edge connectivity
    preflow_push:   0.53 seconds
    ford_fulkerson: 0.29 seconds

Name: Power law configuration model
Type: Graph
Number of nodes: 970
Number of edges: 2413
Average degree:   4.9753
Mean and std of computing the maximum flow among 100 pairs of nodes
  Kind of capacity: unit
    preflow_push:   0.021 (0.0054) seconds
    ford_fulkerson: 0.008 (0.0024) seconds
  Kind of capacity: random ints
    preflow_push:   0.021 (0.0073) seconds
    ford_fulkerson: 0.008 (0.0019) seconds
Time computing node connectivity
    preflow_push:   49.52 seconds
    ford_fulkerson: 12.74 seconds
Time computing edge connectivity
    preflow_push:   13.25 seconds
    ford_fulkerson: 5.05 seconds

As you can see, preflow_push is quite slower than ford_fulkerson for 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 that preflow_push outperformed ford_fulkerson. Not sure why is that. Any ideas?

Note that the difference in time is bigger in node_connectivity computations. Maybe the structure of the auxiliary digraph used in connectivity algorithms has something to do with ford_fulkerson outperforming preflow_push?

I did not modify analogously the cut functions because the residual graph returned by preflow_push needs preprocessing (remove all edges that exhausted their capacity), thus increasing even more the time required for cuts using the preflow_push algorithm. However if someone thinks it would be useful, I can do the necessary changes.

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

ysitu commented Apr 5, 2014

Copy link
Copy Markdown
Contributor

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 ford_fulkerson). For example, Dinic's algorithm runs in O(m √_n_) time (see Even & Tarjan and Ahuja & Orlin for details) on unit-capacity networks, whereas preflow-push algorithms run in O(nm) time. In practice, the actual number of flow augmentations is easily bounded by the degree of the source. For sparse graphs, this further helps augmenting path algorithms score wins over preflow-push algorithms.

@ysitu

ysitu commented Apr 5, 2014

Copy link
Copy Markdown
Contributor

Specifically to the code, I believe that preflow_push_impl is useful for max_flow, min_cut, minimum_st_edge_cut (and thus the s-t case of minimum_edge_cut), i.e., weighted problems without obvious low (e.g., O(n)) upper bounds on minimum cuts. minimum_st_node_cut, minimum_node_cut and the source/sink-less case of minimum_edge_cut fall out of the scope.

@jtorrents

Copy link
Copy Markdown
Member Author

If I understand it correctly, the existing code for connectivity handles unweighted problems.

Yes that's right. By the way, a great thing about your PR #1094 is that it handles (only positive) weighted graphs.

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 ford_fulkerson). For example, Dinic's algorithm runs in O(m √n) time (see Even & Tarjan and Ahuja & Orlin for details) on unit-capacity networks, whereas preflow-push algorithms run in O(nm) time. In practice, the actual number of flow augmentations is easily bounded by the degree of the source. For sparse graphs, this further helps augmenting path algorithms score wins over preflow-push algorithms.

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
http://jorlin.scripts.mit.edu/docs/publications/33-distance%20directed%20aug.pdf

@jtorrents

Copy link
Copy Markdown
Member Author

Specifically to the code, I believe that preflow_push_impl is useful for max_flow, min_cut, minimum_st_edge_cut (and thus the s-t case of minimum_edge_cut), i.e., weighted problems without obvious low (e.g., O(n)) upper bounds on minimum cuts. minimum_st_node_cut, minimum_node_cut and the source/sink-less case of minimum_edge_cut fall out of the scope.

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 preflow_push. It is also nice that the user can easily experiment with different max flow algorithms.

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.

@ysitu

ysitu commented Apr 5, 2014

Copy link
Copy Markdown
Contributor

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 ford_fulkerson for general problems. What is lacking is an implementation.

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

Copy link
Copy Markdown
Member Author

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 ford_fulkerson for general problems. What is lacking is an implementation.

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.

@ysitu

ysitu commented Apr 8, 2014

Copy link
Copy Markdown
Contributor

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.

@jtorrents

Copy link
Copy Markdown
Member Author

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 min(degree(s), degree(t))?

@ysitu

ysitu commented Apr 9, 2014

Copy link
Copy Markdown
Contributor

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.

@jtorrents

Copy link
Copy Markdown
Member Author

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 degree >= edge connectivity >= node connectivity). If I understand it correctly, what we would need is to implement this kind of cutoff in the flow algorithm. I guess that we could add a parameter for the "cutoff" and terminate the algorithm as soon as R.node[t][excess] == cutoff. It would be great if you can implement this, if not I'll give it a try (hopefully with your guidance).

@ysitu

ysitu commented Apr 11, 2014

Copy link
Copy Markdown
Contributor

I will take a look at adding cutoff. Here is the plan:

  1. cutoff is just a hint. An algorithm may decide to go over it or simply ignore it.
  2. preflow_push_flow always ignores it or raises an exception if cutoff is finite.
  3. ford_fulkerson will not be modified. A replacement edmonds_karp will be provided.

jtorrents added a commit to jtorrents/networkx that referenced this pull request Apr 12, 2014
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.
@jtorrents

Copy link
Copy Markdown
Member Author

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 ford_fulkerson as a legacy implementation, your work in edmonds_karp looks great!

@ysitu

ysitu commented Apr 13, 2014

Copy link
Copy Markdown
Contributor

I think that I will put off adding "cutoff" until #1102 is resolved.

jtorrents added a commit to jtorrents/networkx that referenced this pull request Apr 18, 2014
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.
@hagberg hagberg added this to the networkx-1.9 milestone Apr 19, 2014
@ysitu

ysitu commented Apr 24, 2014

Copy link
Copy Markdown
Contributor

Now that shortest_augmenting_path is being added in #1102, there is no need to select an algorithm for computing connectivity. SAP should always be used.

@jtorrents

Copy link
Copy Markdown
Member Author

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 two_phase and cutoff I'd also like to clean up a bit the code and organize it better), so I'm closing this PR now and will open another shortly.

@jtorrents jtorrents closed this Apr 24, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants