Skip to content

Add a preflow–push maximum flow algorithm - #1082

Merged
hagberg merged 22 commits into
networkx:masterfrom
ysitu:preflow_push
Mar 28, 2014
Merged

hagberg merged 22 commits into
networkx:masterfrom
ysitu:preflow_push

Conversation

@ysitu

@ysitu ysitu commented Mar 22, 2014

Copy link
Copy Markdown
Contributor

I noticed that there was a two-year-old Issue (#550) about preflow–push (or push–relabel) algorithms, but apparently those algorithms were never included in any releases. Comments under the Issue showed some discouraging running times. But I think that it is still useful to have them integrated because many papers (e.g., [1]) have suggested the advantage of preflow–push algorithms over the Ford–Fulkerson algorithm.

I was unable to locate the implementations referenced in the Issue, so I went ahead to write one. I picked the highest-label variant with the global relabeling and gap heuristics.

Running compare_relabel.py from the Issue shows an improvement:

nx.preflow_push                    0.572 s
nx.ford_fulkerson                  1.461 s

Running the 18 DIMACS cases used in [1] shows greater speedups for the larger ones:

==========================================================
                                   preflow_push
        ford_fulkerson   --------------------------------
                          @003a541   @e4f35ad   @cb655f9
----------------------------------------------------------
 GL1            1.38 s      0.20 s     0.15 s     0.12 s
 GL2            4.05 s      0.33 s     0.28 s     0.23 s
 GL3           17.62 s      1.56 s     0.99 s     0.80 s
 GL4           66.50 s      1.97 s     1.81 s     1.27 s
 GL5          310.80 s      5.84 s     4.05 s     2.96 s
 GL6         1532.13 s     15.52 s     9.29 s     6.82 s
----------------------------------------------------------
 GW1            0.52 s      0.44 s     0.43 s     0.34 s
 GW2            2.52 s      1.19 s     1.12 s     0.72 s
 GW3            7.86 s      2.42 s     2.30 s     1.83 s
 GW4           34.39 s      5.90 s     5.57 s     4.14 s
 GW5          166.30 s     17.20 s    15.61 s    11.23 s
 GW6          870.59 s     43.72 s    41.07 s    29.68 s
----------------------------------------------------------
 WLM1           0.10 s      0.04 s     0.03 s     0.03 s
 WLM2           0.43 s      0.16 s     0.09 s     0.06 s
 WLM3           1.39 s      0.16 s     0.16 s     0.12 s
 WLM4           7.89 s      0.46 s     0.45 s     0.34 s
 WLM5          43.31 s      2.32 s     1.47 s     1.16 s
 WLM6         190.64 s      5.72 s     4.18 s     3.28 s
==========================================================

Interestingly, the running times of the GL and WLM cases above, measured using Python 3.4 on a Xeon E5530, are longer than those found in [1], which were collected almost 20 years ago on a DECsystem 5000. I suspect that constant factor optimization is possible such as reducing dictionary lookups. But perhaps the user can take a look at a C++ implementation like Boost.Graph if he/she needs to deal with very large problems.

Reference
  1. Ahuja, R.; Kodialam, M.; Mishra, A. K.; Orlin, J. B. (1997). "Computational investigations of maximum flow algorithms". European Journal of Operational Research, 97 (3): 509–542.

@hagberg

hagberg commented Mar 23, 2014

Copy link
Copy Markdown
Member

Wow, that is really fantastic. Thank you for implementing this and making such a beautiful pull request.
I think it is ready to merge. @loicseguin do you have any comments?

ysitu added 7 commits March 23, 2014 21:57
These networks are generated using the same generators and parameters as
in the 1st DIMACS Implementation Challenge. But they are not the same as
the actual DIMACS instances due to different random numbers being used.
This ensures that during phase 1, if a node is found to be disconnected
from the sink in the residual network, it will not be further processed
until phase 2.
The interface is intended to be internal and for use in the minimum cut
algorithm.
@jtorrents

Copy link
Copy Markdown
Member

Hi ysitu,

This is great! Thank you very much for your work. The Travis failures on your PR are because you did not add the actual *.gpickle.bz2 files into the test folder.

It is very handy that you refactored your code to allow retrieving the residual network for minimum cut and friends. Given the benchmarks that you posted, I think we should use this implementation as the default in the connectivity module. I'll look into that when this PR is merged.

Salut!

@ysitu

ysitu commented Mar 24, 2014

Copy link
Copy Markdown
Contributor Author

I have no idea how to fix this. I did add the .bz2 files in ysitu/networkx@75d5c64.

@Midnighter

Copy link
Copy Markdown

You may have to adjust the setup.py script to also install data files and not just Python scripts.

@jtorrents

Copy link
Copy Markdown
Member

Ysitu,

You are right, the files are included. I've only checked the Travis log and saw a "File not found error". You also have to edit setup.py to include the bz2 files:

diff --git a/setup.py b/setup.py
index 610026e..4254a53 100644
--- a/setup.py
+++ b/setup.py
@@ -95,7 +95,7 @@ package_data     = {
     'networkx.algorithms.community': ['tests/*.py'],
     'networkx.algorithms.components': ['tests/*.py'],
     'networkx.algorithms.connectivity': ['tests/*.py'],
-    'networkx.algorithms.flow': ['tests/*.py'],
+    'networkx.algorithms.flow': ['tests/*.py', 'tests/*.bz2'],
     'networkx.algorithms.traversal': ['tests/*.py'],
     'networkx.algorithms.isomorphism': ['tests/*.py','tests/*.*99'],
     'networkx.algorithms.link_analysis': ['tests/*.py'],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some DocTest failures in python 3 in lines like this. The order of the keys in dictionaries changes between python versions (and also between platforms). Maybe we could just skip the last two lines of the example and end with >>> flow, F = nx.preflow_push(G, 'x', 'y') or do something more elaborated such as:

print(flow)
for node, nbrs in sorted(F.items()):
    print(node)
    for n, value in sorted(nbrs.items()):
         print('  {0} : {1}'.format(n, value))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that, too. I have changed it to print just one entry.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2014-03-24 16:39 GMT+01:00 ysitu [email protected]:

In networkx/algorithms/flow/preflow_push.py:

  • Examples

  • import networkx as nx

  • G = nx.DiGraph()

  • G.add_edge('x','a', capacity=3.0)

  • G.add_edge('x','b', capacity=1.0)

  • G.add_edge('a','c', capacity=3.0)

  • G.add_edge('b','c', capacity=5.0)

  • G.add_edge('b','d', capacity=4.0)

  • G.add_edge('d','e', capacity=2.0)

  • G.add_edge('c','y', capacity=2.0)

  • G.add_edge('e','y', capacity=3.0)

  • flow, F = nx.preflow_push(G, 'x', 'y')

  • flow, F

  • (3.0, {'a': {'c': 2.0}, 'c': {'y': 2.0}, 'b': {'c': 0, 'd': 1.0}, 'e': {'y': 1.0}, 'd': {'e': 1.0}, 'y': {}, 'x': {'a': 2.0, 'b': 1.0}})

I noticed that, too. I have changed it to print just one entry.

Great! That also works. Now all tests pass. I think this is ready to merge,
but maybe @loicseguin has some more comments.

Salut!

@ysitu ysitu closed this Mar 24, 2014
@ysitu

ysitu commented Mar 24, 2014

Copy link
Copy Markdown
Contributor Author

Did I just hit the wrong button?

@ysitu ysitu reopened this Mar 24, 2014
@hagberg

hagberg commented Mar 24, 2014

Copy link
Copy Markdown
Member

No worries - I'll reopen it. Or you can too. Thanks for helping fix all of the little details (tests).

@ysitu

ysitu commented Mar 24, 2014

Copy link
Copy Markdown
Contributor Author

My bad. I thought that "Close" button was for closing the comment box.

@loicseguin

Copy link
Copy Markdown
Member

I do not have time to go over the code in details, but this sure looks pretty nice!

I glimpsed over the code and did not see any obvious problems. The runtimes are great!

Jordi, I think this can not only become the default for connectivity, but eventually the default max flow algorithm in nx.

@ysitu

ysitu commented Mar 25, 2014

Copy link
Copy Markdown
Contributor Author

I believe that there is a bug in the global_relabel function which the tests happen to not cover.

@ysitu

ysitu commented Mar 27, 2014

Copy link
Copy Markdown
Contributor Author

I think that the code is done. For some reason Sphinx does not run on my machine, so I do not know what the docs look like. But I suppose that it should be an easy fix should there be any doc problems.

@jtorrents

Copy link
Copy Markdown
Member

Ysitu,

This is one of the best PR that we had at NetworkX, thank you again for this. Regarding the documentation, I've built it in my system and found a small issue; there are 3 typos in each docstring that prevent correct rendering. I'm not sure how to properly do a pull request on your pull request, so I add the patch below. The changes are minimal, it will probably be faster if you apply them by hand.

Salut!

diff --git a/networkx/algorithms/flow/preflow_push.py b/networkx/algorithms/flow/preflow_push.py
index 85513d4..8bc15a4 100644
--- a/networkx/algorithms/flow/preflow_push.py
+++ b/networkx/algorithms/flow/preflow_push.py
@@ -397,7 +397,7 @@ def preflow_push(G, s, t, capacity='capacity', global_relabel_freq=1):
     """Find a maximum single-commodity flow using the highest-label
     preflow-push algorithm.

-    This algorithm has a running time of `O(n^2 m^(1/2))` for `n` nodes and
+    This algorithm has a running time of `O(n^2 m^{(1/2)})` for `n` nodes and
     `m` edges.


@@ -414,13 +414,13 @@ def preflow_push(G, s, t, capacity='capacity', global_relabel_freq=1):
     t : node
         Sink node for the flow.

-    capacity: string
+    capacity : string
         Edges of the graph G are expected to have an attribute capacity
         that indicates how much flow the edge can support. If this
         attribute is not present, the edge is considered to have
         infinite capacity. Default value: 'capacity'.

-    global_relabel_freq: integer, float
+    global_relabel_freq : integer, float
         Relative frequency of applying the global relabeling heuristic to speed
         up the algorithm. If it is None, the heuristic is disabled. Default
         value: 1.
@@ -470,7 +470,7 @@ def preflow_push_value(G, s, t, capacity='capacity', global_relabel_freq=1):
     """Find a maximum single-commodity flow using the highest-label preflow-
     push algorithm.

-    This algorithm has a running time of `O(n^2 m^(1/2))` for `n` nodes and
+    This algorithm has a running time of `O(n^2 m^{(1/2)})` for `n` nodes and
     `m` edges.


@@ -487,13 +487,13 @@ def preflow_push_value(G, s, t, capacity='capacity', global_relabel_freq=1):
     t : node
         Sink node for the flow.

-    capacity: string
+    capacity : string
         Edges of the graph G are expected to have an attribute capacity
         that indicates how much flow the edge can support. If this
         attribute is not present, the edge is considered to have
         infinite capacity. Default value: 'capacity'.

-    global_relabel_freq: integer, float
+    global_relabel_freq : integer, float
         Relative frequency of applying the global relabeling heuristic to speed
         up the algorithm. If it is None, the heuristic is disabled. Default
         value: 1.
@@ -539,7 +539,7 @@ def preflow_push_flow(G, s, t, capacity='capacity', global_relabel_freq=1):
     """Find a maximum single-commodity flow using the highest-label preflow-
     push algorithm.

-    This algorithm has a running time of `O(n^2 m^(1/2))` for `n` nodes and
+    This algorithm has a running time of `O(n^2 m^{(1/2)})` for `n` nodes and
     `m` edges.


@@ -556,13 +556,13 @@ def preflow_push_flow(G, s, t, capacity='capacity', global_relabel_freq=1):
     t : node
         Sink node for the flow.

-    capacity: string
+    capacity : string
         Edges of the graph G are expected to have an attribute capacity
         that indicates how much flow the edge can support. If this
         attribute is not present, the edge is considered to have
         infinite capacity. Default value: 'capacity'.

-    global_relabel_freq: integer, float
+    global_relabel_freq : integer, float
         Relative frequency of applying the global relabeling heuristic to speed
         up the algorithm. If it is None, the heuristic is disabled. Default
         value: 1.

@jtorrents

Copy link
Copy Markdown
Member

Great! Now the doctrings render correctly. I think this is ready to merge.

@hagberg

hagberg commented Mar 28, 2014

Copy link
Copy Markdown
Member

I'll merge this. Thank you for this great contribution @ysitu. I hope you will consider making other contributions in the future.

@hagberg hagberg added this to the networkx-1.9 milestone Mar 28, 2014
hagberg added a commit that referenced this pull request Mar 28, 2014
Add a preflow–push maximum flow algorithm
@hagberg
hagberg merged commit ffea96a into networkx:master Mar 28, 2014
@ysitu
ysitu deleted the preflow_push branch March 29, 2014 00:44
@ysitu ysitu added the 1.9 label May 13, 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.

5 participants