Repository navigation
Add Stoer–Wagner minimum cut algorithm - #1094
Conversation
|
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 Salut! |
|
Looks great. Ready for merging, I think. Hold till Monday? |
|
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 Thank you very much for your work. This PR is really beautiful. Salut! |
|
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! |
|
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)). |
|
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? |
|
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. 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 |
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 ! |
|
@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 |
There appears to be something called |
|
@hagberg Storage is not the problem. The time overhead brought about by inflated heap sizes is. I did test the performance of @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. |
|
OK thanks @ysitu for clarifying that. I appreciate your work in adding those auxiliary data structures. |
|
Any further comments? |
Add Stoer–Wagner minimum cut algorithm
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