Skip to content

Add Stoer–Wagner minimum cut algorithm - #1094

Merged
chebee7i merged 4 commits into
networkx:masterfrom
ysitu:stoer_wagner
Apr 9, 2014
Merged

chebee7i merged 4 commits into
networkx:masterfrom
ysitu:stoer_wagner

Conversation

@ysitu

@ysitu ysitu commented Apr 4, 2014

Copy link
Copy Markdown
Contributor

This is an implementation of the Stoer–Wagner algorithm for finding weighted minimum cuts of undirected graphs. Because it obviously does not work for the NP-complete maximum cut problem, only nonnegative weights are allowed. Also included are heap implementations that provide in-place key changes, on which the algorithm depends.

Frankly speaking, the measured running times do not look very pleasing to me considering that the asymptotic running time is just a little over Θ(nm + _n_² log n). (It would have been precisely Θ(nm + _n_² log n) had I used Fibonacci heaps, but they apparently felt slower than pairing heaps and binary heaps.) For a 500-node 5050-edge random graph, it measures 4.7 seconds (with binary heaps) and 8.4 seconds (with pairing heaps) with Python 3.4 on my machine, although running it under PyPy 2.2.1 shaves the time to 1.6 seconds when pairing heaps are used.

Reference
  1. Stoer, M.; Wagner, F. (1997). "A simple min-cut algorithm". Journal of the ACM 44 (4): 585–591.

@jtorrents

Copy link
Copy Markdown
Member

Wow! this is really beautiful! I'll look at it more closely, but in a first sight this all looks amazing (the tests, the code, the comments, the documentation). Thank you very much for this PR @ysitu, you rock!

By the way, speaking of speed, by working on #1095 I've found out that preflow_push algorithm is slower than the ford_fulkerson for connectivity algorithms. If you have time, take a look at it.

Salut!

@chebee7i

chebee7i commented Apr 5, 2014

Copy link
Copy Markdown
Member

Looks great. Ready for merging, I think. Hold till Monday?

@jtorrents

Copy link
Copy Markdown
Member

I've look at it more closely, and I have a couple of comments. Regarding what is returned, maybe the edge cutset is more useful than the node partition? The former is smaller and this can make a difference in big graphs. Also I'd prefer functions that return only one thing, but I'm not sure if it is useful enough to have two separate functions for the cut set (or the partitions) and the value.

Also you need to add stoer_wagner function to the connectivity documentation. You can do something like [1](which renames cut functions to flow based cut functions).

Thank you very much for your work. This PR is really beautiful.

Salut!
[1]

jtorrents@saturn:~/projects/dev/networkx$ git diff doc/source/reference/algorithms.connectivity.rst
diff --git a/doc/source/reference/algorithms.connectivity.rst b/doc/source/reference/algorithms.connectiv
index 89108f1..9fa5790 100644
--- a/doc/source/reference/algorithms.connectivity.rst
+++ b/doc/source/reference/algorithms.connectivity.rst
@@ -5,8 +5,8 @@ Connectivity
 .. automodule:: networkx.algorithms.connectivity


-Connectivity functions
-----------------------
+Flow based Connectivity
+-----------------------
 .. automodule:: networkx.algorithms.connectivity.connectivity
 .. autosummary::
    :toctree: generated/
@@ -18,8 +18,8 @@ Connectivity functions
    edge_connectivity
    all_pairs_node_connectivity_matrix

-Cut functions
--------------
+Flow based Cuts
+---------------
 .. automodule:: networkx.algorithms.connectivity.cuts
 .. autosummary::
    :toctree: generated/
@@ -29,3 +29,11 @@ Cut functions
    minimum_st_edge_cut
    minimum_edge_cut

+Stoer Wagner cut
+----------------
+.. automodule:: networkx.algorithms.connectivity.stoer_wagner
+.. autosummary::
+   :toctree: generated/
+
+   stoer_wagner
+

@argriffing

Copy link
Copy Markdown

just want to vote +1 to this function, I've implemented it badly from the primary literature using dense numpy adjacency matrices without real heaps, and I would be happy if it is in networkx!

@ysitu

ysitu commented Apr 7, 2014

Copy link
Copy Markdown
Contributor Author

I find it better to return a node partition than than an edge set because: 1) that is how cuts are defined; 2) it is much easier to find the cutset from a node partition than the other way round; 3) the output has predictable size (== len(G)).

@hagberg

hagberg commented Apr 7, 2014

Copy link
Copy Markdown
Member

Can you comment more on the use of the special heaps? I"m not familiar with this algorithm at all and was wondering why the Python heapq couldn't be used. It must be the "get" function? Is the queue performance the bottleneck?

@ysitu

ysitu commented Apr 7, 2014

Copy link
Copy Markdown
Contributor Author

Heap performance is indeed the bottleneck. In fact, the bulk of the algorithm is about manipulating heaps. In particular, the algorithm calls for an efficient implementation of the decrease-key operation. heapq does not support it natively, but it can be emulated in the same way as is done in single_source_dijkstra by allowing stale items. BinaryHeap is an abstraction for that.

The downside of allowing stale items is that it inflates the actual size of the heap to O(n + m) while there are only O(n) effective elements. A heap implementation that supports decrease-key natively can avoid this issue and also perform the operation in sublogarithmic time. This is what PairingHeap does. The reason why I did not make it the default is that the performance gain when running under CPython is not significant for problems that take less than 10 seconds. For those who have the luxury to use ahead-of-/just-in-time compilation (PyPy, etc.), they will see a benefit even for problems that run in ~1 second.

@jtorrents

Copy link
Copy Markdown
Member

I find it better to return a node partition than than an edge set because: 1) that is how cuts are defined; 2) it is much easier to find the cutset from a node partition than the other way round; 3) the output has predictable size (== len(G)).

Well, I'm not quite convinced by 1 (because you can properly define a cut in terms of the cutset) and 3 (I don't see a problem with results not having predictable size), but 2 is clearly true. And since this is only a small and lateral issue, I think we can merge this as is if @hagberg agrees. Great work @ysitu !

@hagberg

hagberg commented Apr 7, 2014

Copy link
Copy Markdown
Member

@ysitu did you test the performance with the Python heapq? I wonder if for many cases it is the fastest even if the storage complexity is bad?

If we care about storage we might consider not copying the graph if the user doesn't need it anymore.

Is the HeapDict code usable for this? https://pypi.python.org/pypi/HeapDict

@argriffing

Copy link
Copy Markdown

A heap implementation that supports decrease-key natively can avoid this issue
and also perform the operation in sublogarithmic time.

There appears to be something called _siftdown http://stackoverflow.com/questions/1465662/how-can-i-implement-decrease-key-functionality-in-pythons-heapq implementing native heapq decrease-key but it's not in the public interface.

@ysitu

ysitu commented Apr 7, 2014

Copy link
Copy Markdown
Contributor Author

@hagberg Storage is not the problem. The time overhead brought about by inflated heap sizes is. I did test the performance of heapq. Written in C in CPython, it is indeed an efficient heap implementation. That is why it is the default for stoer_wagner (wrapped as BinaryHeap to provide a uniform interface). HeapDict, however, will unlikely help because it is written in pure Python. I have a unpublished branch that implements binary heaps (as generic d-heaps with d = 2) and also Fibonacci heaps in Python. It turns out that pairing heaps are the fastest, followed by Fibonacci heaps, quaternary heaps and binary heaps in order. Based on these tests I decided to go with the current BinaryHeap and PairingHeap for implementing stoer_wagner. The former is intended for CPython, while the latter for PyPy and alike.

@argriffing There is a bigger problem. You need to maintain an index into the heap array. That logic needs to be baked into _siftup/_siftdown to make sense efficiency-wise.

@hagberg

hagberg commented Apr 7, 2014

Copy link
Copy Markdown
Member

OK thanks @ysitu for clarifying that. I appreciate your work in adding those auxiliary data structures.
I think we are done here unless anyone else has comments.

@ysitu

ysitu commented Apr 9, 2014

Copy link
Copy Markdown
Contributor Author

Any further comments?

chebee7i added a commit that referenced this pull request Apr 9, 2014
Add Stoer–Wagner minimum cut algorithm
@chebee7i
chebee7i merged commit 2aaf3c2 into networkx:master Apr 9, 2014
@hagberg hagberg self-assigned this Apr 9, 2014
@hagberg hagberg added this to the networkx-1.9 milestone Apr 9, 2014
@ysitu
ysitu deleted the stoer_wagner branch April 9, 2014 20:55
@ysitu ysitu mentioned this pull request Apr 18, 2014
@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