Skip to content

Fix bugs in all_node_cuts, address #3025 - #3039

Merged
dschult merged 6 commits into
networkx:masterfrom
wuhaochen:patch-all-node-cuts
Jul 25, 2018
Merged

dschult merged 6 commits into
networkx:masterfrom
wuhaochen:patch-all-node-cuts

Conversation

@wuhaochen

@wuhaochen wuhaochen commented Jun 28, 2018 •

Copy link
Copy Markdown
Contributor

Address #3025.

I made quite some changes to the original implementation and this probably need to be carefully reviewed.

The code correctly find much more cuts from torrents_and_ferraro_graph and doesn't need to deal with cycle graph separately. However it's also significantly slower than before, so I turned off a few tests for now.

I will explain a bit about the changes later and probably add more comments, but I'd like to get some feedback.

@wuhaochen

Copy link
Copy Markdown
Contributor Author

I have a few specific questions about some design decisions to make:

  1. I don't think we need specific code to take care of the corner cases in the beginning. It might perform better in such cases but those cases are probably rare and we suffer from those extra checking in other cases. I got an impression those were introduced to attempt fixing the (bugged) implementation before, so I think this is a good time to remove them since the new code can correctly address them (but slower). If our users do have network with such structures, it is not necessary to use our algorithm to find them as they are easy to find.

  2. The algorithm is much slower now, I'm not sure if the performance can be optimized much since the algorithm we use has a time complexity of O(2^kn^3), which is exponential to the network connectivity k. As a consequence, some of our test cases are not appropriate to be used, since they have a relative large k. To give a concrete example, the test cases we used in the alternative flow graph_example_1, nx.davis_southern_women_graph were not finished after 4 hours in my laptop.
    I have two ideas about this, first is that we can check if the first few cuts we find are valid cuts for larger and more connected graph, or we can limit the test for only small graphs.

Besides these two questions, I would also hope the code to be reviewed a bit since I think the change is quite substantial. I wonder if you guys can take some look. Thank you. @jtorrents @dschult

@wuhaochen wuhaochen changed the title WIP: Fix bugs in all_node_cuts, address #3025 Fix bugs in all_node_cuts, address #3025 Jul 17, 2018
@wuhaochen

Copy link
Copy Markdown
Contributor Author

I took the liberty for these design questions and I think it's ready to merge now but let me know if you have issues with it.

@dschult

dschult commented Jul 22, 2018 •

Copy link
Copy Markdown
Member

Does seen need to be a list in this function? It might speed it up quite a bit if a set is used.
I guess that would require node_cut to be a frozen_set. And maybe it is irrelevant -- I haven't done any profiling.

If I understand correctly, the change that fixes the algorithm is to move out of the loop over antichains the code that puts x and v in the auxillary graphs. I don't know the algorithm well, but: once we yield a node_cut do we need to consider any other antichains for those values of x and v? That is, is it possible to have another antichain produce a node_cut with that same value of x, v, H and R?

Thanks for this -- it helps a lot.

@wuhaochen

wuhaochen commented Jul 23, 2018 •

Copy link
Copy Markdown
Contributor Author

I did some basic profiling on very small graph and it seems that there is no obvious optimization available. I don't think the type of seen is a big issue here.
The bottleneck is on

S = {n for n, scc in cmap.items() if scc in antichain}
S |= {x for n in S for x in R_closure.predecessors(n)}

Each of them takes about 20% execution time.

There seems to have multiple incorrect implementation in the original code and one of them is ignoring other antichains, original bug report is about missing some cuts and those cuts are generated by other antichains we skipped.

Profiling on first 20 cuts in `torrents_and_ferraro_graph`
         130017711 function calls (129791466 primitive calls) in 89.269 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
       23    0.000    0.000    0.009    0.000 <decorator-gen-232>:1(is_connected)
        9    0.000    0.000    0.000    0.000 <decorator-gen-236>:1(strongly_connected_components)
        9    0.000    0.000    0.024    0.003 <decorator-gen-248>:1(condensation)
       18    0.000    0.000    2.313    0.129 <decorator-gen-300>:1(transitive_closure)
        9    0.000    0.000    0.000    0.000 <decorator-gen-304>:1(antichains)
        1    0.001    0.001   89.269   89.269 <ipython-input-12-f883bdad3041>:1(for_profile)
        1    0.000    0.000   89.269   89.269 <string>:1(<module>)
  1990357    1.640    0.000    2.102    0.000 _abcoll.py:386(__contains__)
    16871    0.036    0.000    0.053    0.000 _abcoll.py:412(items)
    33660    0.069    0.000    0.105    0.000 _abcoll.py:416(values)
       23    0.000    0.000    0.009    0.000 connected.py:121(is_connected)
     1940    0.001    0.000    0.008    0.000 connected.py:162(<genexpr>)
     1940    0.003    0.000    0.008    0.000 connected.py:199(_plain_bfs)
        1    0.000    0.000    0.206    0.206 connectivity.py:213(node_connectivity)
       98    0.001    0.000    0.199    0.002 connectivity.py:33(local_node_connectivity)
 2292/698    0.007    0.000    0.016    0.000 copy.py:145(deepcopy)
     1496    0.000    0.000    0.000    0.000 copy.py:198(_deepcopy_atomic)
  699/698    0.002    0.000    0.013    0.000 copy.py:253(_deepcopy_dict)
     2195    0.004    0.000    0.005    0.000 copy.py:267(_keep_alive)
       98    0.000    0.000    0.000    0.000 coreviews.py:284(__init__)
        2    0.000    0.000    0.000    0.000 coreviews.py:288(__len__)
      196    0.000    0.000    0.000    0.000 coreviews.py:289(<genexpr>)
      101    0.000    0.000    0.000    0.000 coreviews.py:291(__iter__)
      664    0.000    0.000    0.000    0.000 coreviews.py:298(<genexpr>)
        1    0.000    0.000    0.000    0.000 coreviews.py:325(__init__)
       97    0.000    0.000    0.000    0.000 coreviews.py:342(__getitem__)
      384    0.000    0.000    0.000    0.000 coreviews.py:344(new_node_ok)
 21070946    2.450    0.000    2.450    0.000 coreviews.py:45(__init__)
  8547580    2.075    0.000    2.860    0.000 coreviews.py:51(__iter__)
  2260853    0.498    0.000    0.498    0.000 coreviews.py:54(__getitem__)
 10551206    4.344    0.000    5.485    0.000 coreviews.py:81(__getitem__)
      206    0.001    0.000    0.003    0.000 dag.py:120(topological_sort)
        9    0.000    0.000    0.000    0.000 dag.py:177(<dictcomp>)
       18    0.004    0.000    2.313    0.128 dag.py:347(transitive_closure)
   220778    0.068    0.000    1.783    0.000 dag.py:377(<genexpr>)
   750790    2.919    0.000    8.918    0.000 dag.py:424(antichains)
    68/59    0.000    0.000    2.346    0.040 decorators.py:55(_not_implemented_for)
     1979    0.002    0.000    0.002    0.000 depth_first_search.py:272(dfs_preorder_nodes)
   222757    0.222    0.000    1.715    0.000 depth_first_search.py:319(<genexpr>)
   761071    0.712    0.000    1.493    0.000 depth_first_search.py:322(dfs_labeled_edges)
       92    0.000    0.000    0.000    0.000 digraph.py:1032(is_multigraph)
      101    0.000    0.000    0.000    0.000 digraph.py:1036(is_directed)
       19    0.000    0.000    0.000    0.000 digraph.py:1040(fresh_copy)
       18    0.000    0.000    0.017    0.001 digraph.py:1054(copy)
     1997    0.001    0.000    0.001    0.000 digraph.py:1135(<genexpr>)
     4688    0.002    0.000    0.003    0.000 digraph.py:1136(<genexpr>)
        1    0.000    0.000    0.022    0.022 digraph.py:1279(reverse)
      199    0.000    0.000    0.006    0.000 digraph.py:1295(<genexpr>)
      500    0.001    0.000    0.011    0.000 digraph.py:1296(<genexpr>)
       32    0.000    0.000    0.000    0.000 digraph.py:246(__init__)
 10519146    4.244    0.000    5.552    0.000 digraph.py:299(adj)
      397    0.000    0.000    0.000    0.000 digraph.py:318(succ)
      170    0.000    0.000    0.000    0.000 digraph.py:339(pred)
      396    0.001    0.000    0.001    0.000 digraph.py:355(add_node)
       30    0.006    0.000    0.013    0.000 digraph.py:401(add_nodes_from)
     2242    0.006    0.000    0.007    0.000 digraph.py:548(add_edge)
     2017    0.406    0.000    2.331    0.001 digraph.py:614(add_edges_from)
        9    0.002    0.000    0.002    0.000 digraph.py:709(remove_edges_from)
 11701297    3.466    0.000    4.474    0.000 digraph.py:768(predecessors)
      227    0.000    0.000    0.001    0.000 digraph.py:775(edges)
       18    0.000    0.000    0.000    0.000 digraph.py:919(in_degree)
      170    0.038    0.000    0.330    0.002 edmondskarp.py:104(edmonds_karp_impl)
      170    0.000    0.000    0.330    0.002 edmondskarp.py:131(edmonds_karp)
      170    0.003    0.000    0.124    0.001 edmondskarp.py:17(edmonds_karp_core)
      421    0.011    0.000    0.021    0.000 edmondskarp.py:26(augment)
      494    0.034    0.000    0.099    0.000 edmondskarp.py:49(bidirectional_bfs)
        1    0.000    0.000    0.000    0.000 filters.py:28(hide_nodes)
      680    0.000    0.000    0.000    0.000 filters.py:30(<lambda>)
        1    0.000    0.000    0.000    0.000 filters.py:38(hide_edges)
        1    0.000    0.000    0.000    0.000 filters.py:39(<setcomp>)
      368    0.000    0.000    0.000    0.000 filters.py:40(<lambda>)
        1    0.000    0.000    0.000    0.000 function.py:435(restricted_view)
        9    0.000    0.000    0.000    0.000 function.py:589(set_node_attributes)
        1    0.000    0.000    0.000    0.000 function.py:67(number_of_nodes)
        1    0.000    0.000    0.001    0.001 function.py:72(number_of_edges)
        1    0.000    0.000    0.001    0.001 function.py:77(density)
      998    0.000    0.000    0.000    0.000 graph.py:1035(has_edge)
        2    0.000    0.000    0.000    0.000 graph.py:1076(neighbors)
        2    0.000    0.000    0.000    0.000 graph.py:1120(edges)
        3    0.000    0.000    0.000    0.000 graph.py:1243(degree)
       47    0.000    0.000    0.000    0.000 graph.py:1302(is_multigraph)
       51    0.000    0.000    0.000    0.000 graph.py:1306(is_directed)
       20    0.000    0.000    0.000    0.000 graph.py:1310(fresh_copy)
       20    0.000    0.000    0.024    0.001 graph.py:1324(copy)
     2000    0.001    0.000    0.001    0.000 graph.py:1405(<genexpr>)
     8020    0.003    0.000    0.004    0.000 graph.py:1406(<genexpr>)
        1    0.000    0.000    0.001    0.001 graph.py:1623(size)
      100    0.000    0.000    0.001    0.000 graph.py:1659(<genexpr>)
        1    0.000    0.000    0.001    0.001 graph.py:1666(number_of_edges)
      197    0.000    0.000    0.000    0.000 graph.py:1718(nbunch_iter)
       20    0.000    0.000    0.000    0.000 graph.py:259(__init__)
       27    0.000    0.000    0.000    0.000 graph.py:306(adj)
      249    0.000    0.000    0.000    0.000 graph.py:355(__iter__)
      734    0.000    0.000    0.000    0.000 graph.py:373(__contains__)
     2035    0.001    0.000    0.001    0.000 graph.py:387(__len__)
 10519150    6.925    0.000   17.943    0.000 graph.py:404(__getitem__)
       20    0.003    0.000    0.005    0.000 graph.py:475(add_nodes_from)
       20    0.000    0.000    0.000    0.000 graph.py:581(remove_nodes_from)
  1854724    0.912    0.000    1.210    0.000 graph.py:616(nodes)
        1    0.000    0.000    0.000    0.000 graph.py:750(number_of_nodes)
       20    0.012    0.001    0.019    0.001 graph.py:875(add_edges_from)
        1    0.000    0.000    0.000    0.000 graphviews.py:63(__init__)
       49    0.000    0.000    0.001    0.000 iostream.py:195(schedule)
       40    0.000    0.000    0.000    0.000 iostream.py:300(_is_master_process)
       40    0.000    0.000    0.000    0.000 iostream.py:313(_schedule_flush)
       40    0.000    0.000    0.002    0.000 iostream.py:366(write)
       49    0.000    0.000    0.000    0.000 iostream.py:93(_event_pipe)
        1    0.000    0.000    0.000    0.000 kcutsets.py:119(<setcomp>)
   621178   17.157    0.000   17.157    0.000 kcutsets.py:169(<setcomp>)
   621178   18.142    0.000   22.616    0.000 kcutsets.py:170(<setcomp>)
  8911522    2.390    0.000    2.390    0.000 kcutsets.py:176(<genexpr>)
    58585    0.367    0.000    0.795    0.000 kcutsets.py:183(<setcomp>)
        1    0.000    0.000    0.001    0.001 kcutsets.py:219(_is_separating_set)
       20   13.075    0.654   89.236    4.462 kcutsets.py:23(all_node_cuts)
       98    0.000    0.000    0.198    0.002 maxflow.py:175(maximum_flow_value)
       23    0.000    0.000    0.000    0.000 misc.py:199(is_iterator)
       23    0.000    0.000    0.000    0.000 misc.py:208(arbitrary_element)
      402    0.001    0.000    0.001    0.000 reportviews.py:1025(__iter__)
  1854724    0.299    0.000    0.299    0.000 reportviews.py:167(__init__)
        1    0.000    0.000    0.000    0.000 reportviews.py:174(__iter__)
  1854751    0.276    0.000    0.276    0.000 reportviews.py:177(__getitem__)
       21    0.000    0.000    0.000    0.000 reportviews.py:334(__init__)
       21    0.000    0.000    0.000    0.000 reportviews.py:342(__call__)
        1    0.000    0.000    0.000    0.000 reportviews.py:380(__len__)
      300    0.000    0.000    0.000    0.000 reportviews.py:442(__iter__)
      412    0.000    0.000    0.000    0.000 reportviews.py:489(__iter__)
      218    0.000    0.000    0.001    0.000 reportviews.py:642(__init__)
      197    0.000    0.000    0.000    0.000 reportviews.py:649(<lambda>)
    19749    0.002    0.000    0.002    0.000 reportviews.py:655(<lambda>)
      301    0.000    0.000    0.000    0.000 reportviews.py:657(<lambda>)
      218    0.000    0.000    0.000    0.000 reportviews.py:665(__iter__)
    20268    0.007    0.000    0.010    0.000 reportviews.py:666(<genexpr>)
      229    0.000    0.000    0.001    0.000 reportviews.py:906(__init__)
     4378    0.001    0.000    0.001    0.000 reportviews.py:915(__iter__)
      229    0.000    0.000    0.001    0.000 reportviews.py:933(__call__)
       49    0.001    0.000    0.001    0.000 socket.py:333(send)
      206    0.008    0.000    0.019    0.000 strongly_connected.py:27(strongly_connected_components)
        9    0.000    0.000    0.024    0.003 strongly_connected.py:361(condensation)
     1979    0.000    0.000    0.000    0.000 strongly_connected.py:411(<genexpr>)
      435    0.001    0.000    0.002    0.000 strongly_connected.py:414(<genexpr>)
       49    0.000    0.000    0.000    0.000 threading.py:573(isSet)
       49    0.000    0.000    0.000    0.000 threading.py:989(isAlive)
       40    0.000    0.000    0.000    0.000 utf_8.py:15(decode)
     1000    0.000    0.000    0.000    0.000 utils.py:119(<genexpr>)
        2    0.005    0.002    0.015    0.007 utils.py:13(build_auxiliary_node_connectivity)
        2    0.003    0.001    0.010    0.005 utils.py:73(build_residual_network)
       40    0.000    0.000    0.000    0.000 {_codecs.utf_8_decode}
    64921    0.020    0.000    0.020    0.000 {any}
       98    0.000    0.000    0.000    0.000 {callable}
      317    0.000    0.000    0.000    0.000 {hasattr}
     5884    0.001    0.000    0.001    0.000 {id}
       40    0.000    0.000    0.000    0.000 {isinstance}
20472774/20250168    1.868    0.000    1.934    0.000 {iter}
310587/308552    0.027    0.000    0.028    0.000 {len}
   224280    0.025    0.000    0.025    0.000 {method 'add' of 'set' objects}
       49    0.000    0.000    0.000    0.000 {method 'append' of 'collections.deque' objects}
  1002810    0.082    0.000    0.082    0.000 {method 'append' of 'list' objects}
    21379    0.002    0.000    0.002    0.000 {method 'copy' of 'dict' objects}
       40    0.000    0.000    0.000    0.000 {method 'decode' of 'str' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
   246239    0.030    0.000    0.030    0.000 {method 'get' of 'dict' objects}
   750782    0.153    0.000    0.153    0.000 {method 'issubset' of 'set' objects}
   629609    1.930    0.000    1.930    0.000 {method 'items' of 'dict' objects}
      699    0.000    0.000    0.000    0.000 {method 'iteritems' of 'dict' objects}
  1725914    0.249    0.000    0.249    0.000 {method 'pop' of 'list' objects}
      421    0.000    0.000    0.000    0.000 {method 'reverse' of 'list' objects}
   481519    0.088    0.000    0.088    0.000 {method 'update' of 'dict' objects}
  8272707    1.835    0.000    4.227    0.000 {method 'update' of 'set' objects}
     9463    0.002    0.000    0.002    0.000 {min}
   757978    0.082    0.000    0.082    0.000 {next}
       40    0.000    0.000    0.000    0.000 {posix.getpid}
       10    0.000    0.000    0.000    0.000 {range}
        1    0.000    0.000    0.000    0.000 {sorted}
       28    0.001    0.000    0.010    0.000 {sum}

@dschult

dschult commented Jul 24, 2018

Copy link
Copy Markdown
Member

Yes -- the time seems to be mostly in those two set comprehensions and the graph lookups.

All the suggestions here cut the time t less than half. Not a huge savings, but given the now longer time maybe worthwhile...

I think the first set comprehension is taking time because in antichain is slow when antichain is large. We can invert the cmap to make that faster.
The second set comprehension is also slow -- partly because of using the predecessor function but partly because set.update is faster than a nested for loop. Because this is such an inner loop, I suggest we replace predecessor(n) with ._pred[n]. I know we could use .pred[n] (which is a read-only version of _pred) but it might be worth using _pred here.

A couple of other things get about 5-10% improvement: H_Reversed is only used once and then only to do a lookup: H_reversed[u]. We should just use H.pred[u] and not create H_Reversed. Also, both H.pred and H.nodes can be sped up by pre-loading them before the loop over X: Hnodes = H.nodes and Hpred = H._pred

# just after cmap created
inv_cmap = defaultdict(list) .   # at top have to:  from collections import defaultdict
for n, scc in cmap.items():
    inv_cmap[scc].append(n)

============

# replace the first set comp
# S = {n for n, scc in cmap.items() if scc in antichain}
S = set([])
for scc in antichain:
    S.update(inv_cmap[scc])

# replace second set comp
#S |= {x for n in S for x in R_closure.predecessors(n)}
moreS = set([])
for n in S:
    moreS.update(R_closure._pred[n])
S.update(moreS)

===========

# here's the line with H_Reversed replaced
cutset.update((u, w) for w in Hpred[u] if w not in S)

Full patch

Everything included the patch becomes: (is there a better way to show a patch here?)

@@ -2,6 +2,7 @@
 """
 Kanevsky all minimum node k cutsets algorithm.
 """
+from collections import defaultdict
 from operator import itemgetter
 from itertools import combinations
 
@@ -101,8 +102,6 @@
     # for node connectivity.
     H = build_auxiliary_node_connectivity(G)
     mapping = H.graph['mapping']
-    # Reversed H for helping finding cut.
-    H_reversed = H.reverse()
     R = build_residual_network(H, 'capacity')
     kwargs = dict(capacity='capacity', residual=R)
     # Define default flow function
@@ -122,6 +121,8 @@
         seen.append(X)
         yield X
 
+    Hnodes = H.nodes  # for speed
+    Hpred = H._pred  # for speed
     for x in X:
         # step 3: Compute local connectivity flow of x with all other
         # non adjacent nodes in G
@@ -151,6 +152,9 @@
                 # residual flow network R and call it L.
                 L = nx.condensation(R)
                 cmap = L.graph['mapping']
+                inv_cmap = defaultdict(list)
+                for n, scc in cmap.items():
+                    inv_cmap[scc].append(n)
                 # Find the incident nodes in the condensed graph.
                 VE1 = set([cmap[n] for n in VE1])
                 # step 7: Compute all antichains of L;
@@ -166,21 +170,27 @@
                     # define a node partition of the auxiliary digraph H
                     # through taking all of antichain's predecessors in the
                     # transitive closure.
-                    S = {n for n, scc in cmap.items() if scc in antichain}
-                    S |= {x for n in S for x in R_closure.predecessors(n)}
+#                    S = {n for n, scc in cmap.items() if scc in antichain}
+                    S = set([])
+                    for scc in antichain:
+                        S.update(inv_cmap[scc])
+                    moreS = set([])
+                    for n in S:
+                        moreS.update(R_closure._pred[n])
+                    S.update(moreS)
+#                    S |= {x for n in S for x in R_closure.predecessors(n)}
                     if '%sB' % mapping[x] not in S or '%sA' % mapping[v] in S:
                         continue
                     # Find the cutset that links the node partition (S,~S) in H
                     cutset = set()
                     for u in S:
-                        cutset.update((u, w)
-                                      for w in H_reversed[u] if w not in S)
+                        cutset.update((u, w) for w in Hpred[u] if w not in S)
                     # The edges in H that form the cutset are internal edges
                     # (ie edges that represent a node of the original graph G)
-                    if any([H.nodes[u]['id'] != H.nodes[w]['id']
+                    if any([Hnodes[u]['id'] != Hnodes[w]['id']
                             for u, w in cutset]):
                         continue
-                    node_cut = {H.nodes[u]['id'] for u, _ in cutset}
+                    node_cut = {Hnodes[u]['id'] for u, _ in cutset}
 
                     if len(node_cut) == k:
                         # The cut is invalid if it includes internal edges of

@dschult

dschult commented Jul 24, 2018

Copy link
Copy Markdown
Member

It is much better to have the code that doesn't skip those antichains.
I think this is just a hard problem. The patch I put together is not likely to make this useable for graphs it takes too long for -- just speed up the ones we can already do.

There is more work to do here in terms of looking for algorithmic shortcuts and so forth.
But I think this fulfills the goal of correcting the missing cuts, even though the time is longer.

@wuhaochen

wuhaochen commented Jul 24, 2018 •

Copy link
Copy Markdown
Contributor Author

Thanks for all these suggestions, I benchmarked each of these changes and all of them can speed it up a bit.

The most speedup actually comes from not reversing the graph.
Overall the code is taking ~60% percent less time than before, but I don't think we changed the time complexity here.

There is another potential optimization that might help: we can test if not set(antichain).issubset(VE1) while generating the antichains, that will allow us skip many antichains. However, this change will require a customized version of antichains function. I'm not sure if it's worth doing.

Newer profiling results
         60672475 function calls (60447807 primitive calls) in 38.021 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        4    0.000    0.000    0.000    0.000 <decorator-gen-130>:1(complete_graph)
       14    0.000    0.000    0.000    0.000 <decorator-gen-134>:1(empty_graph)
        2    0.000    0.000    0.000    0.000 <decorator-gen-138>:1(path_graph)
       23    0.000    0.000    0.007    0.000 <decorator-gen-232>:1(is_connected)
        9    0.000    0.000    0.000    0.000 <decorator-gen-236>:1(strongly_connected_components)
        9    0.000    0.000    0.026    0.003 <decorator-gen-248>:1(condensation)
       18    0.000    0.000    2.478    0.138 <decorator-gen-300>:1(transitive_closure)
        9    0.000    0.000    0.000    0.000 <decorator-gen-304>:1(antichains)
        1    0.001    0.001    0.030    0.030 <ipython-input-12-47eee439a3f5>:57(torrents_and_ferraro_graph)
       26    0.000    0.000    0.000    0.000 <ipython-input-12-47eee439a3f5>:61(<genexpr>)
        1    0.001    0.001   38.018   38.018 <ipython-input-25-262717bb9c89>:1(for_profile)
       21   10.200    0.486   37.953    1.807 <ipython-input-49-62581d030cd1>:1(all_node_cuts)
  8911522    2.329    0.000    2.329    0.000 <ipython-input-49-62581d030cd1>:163(<genexpr>)
    58585    0.223    0.000    0.292    0.000 <ipython-input-49-62581d030cd1>:170(<setcomp>)
        1    0.000    0.000    0.000    0.000 <ipython-input-49-62581d030cd1>:98(<setcomp>)
        1    0.002    0.002   38.021   38.021 <string>:1(<module>)
  1990357    1.672    0.000    2.144    0.000 _abcoll.py:386(__contains__)
    16897    0.039    0.000    0.058    0.000 _abcoll.py:412(items)
    33660    0.072    0.000    0.110    0.000 _abcoll.py:416(values)
       12    0.000    0.000    0.025    0.002 binary.py:104(disjoint_union)
       12    0.002    0.000    0.012    0.001 binary.py:19(union)
       24    0.000    0.000    0.000    0.000 binary.py:63(add_prefix)
        4    0.000    0.000    0.000    0.000 classic.py:197(complete_graph)
       14    0.000    0.000    0.000    0.000 classic.py:356(empty_graph)
        2    0.000    0.000    0.000    0.000 classic.py:530(path_graph)
       23    0.000    0.000    0.007    0.000 connected.py:121(is_connected)
     1940    0.000    0.000    0.006    0.000 connected.py:162(<genexpr>)
     1940    0.002    0.000    0.006    0.000 connected.py:199(_plain_bfs)
        1    0.000    0.000    0.197    0.197 connectivity.py:213(node_connectivity)
       98    0.001    0.000    0.191    0.002 connectivity.py:33(local_node_connectivity)
        1    0.000    0.000    0.000    0.000 copy.py:113(_copy_with_constructor)
        1    0.000    0.000    0.000    0.000 copy.py:66(copy)
       98    0.000    0.000    0.000    0.000 coreviews.py:284(__init__)
        2    0.000    0.000    0.000    0.000 coreviews.py:288(__len__)
      196    0.000    0.000    0.000    0.000 coreviews.py:289(<genexpr>)
      101    0.000    0.000    0.000    0.000 coreviews.py:291(__iter__)
      664    0.000    0.000    0.000    0.000 coreviews.py:298(<genexpr>)
        1    0.000    0.000    0.000    0.000 coreviews.py:325(__init__)
       97    0.000    0.000    0.000    0.000 coreviews.py:342(__getitem__)
      384    0.000    0.000    0.000    0.000 coreviews.py:344(new_node_ok)
  4529377    0.600    0.000    0.600    0.000 coreviews.py:45(__init__)
   276796    0.083    0.000    0.120    0.000 coreviews.py:51(__iter__)
  2260853    0.512    0.000    0.512    0.000 coreviews.py:54(__getitem__)
  2280422    1.031    0.000    1.305    0.000 coreviews.py:81(__getitem__)
      206    0.001    0.000    0.004    0.000 dag.py:120(topological_sort)
        9    0.000    0.000    0.000    0.000 dag.py:177(<dictcomp>)
       18    0.005    0.000    2.478    0.138 dag.py:347(transitive_closure)
   220778    0.073    0.000    1.906    0.000 dag.py:377(<genexpr>)
   750791    2.978    0.000    9.153    0.000 dag.py:424(antichains)
    20/14    0.000    0.000    0.001    0.000 decorators.py:270(_nodes_or_number)
    68/59    0.000    0.000    2.512    0.043 decorators.py:55(_not_implemented_for)
     1979    0.002    0.000    0.002    0.000 depth_first_search.py:272(dfs_preorder_nodes)
   222757    0.238    0.000    1.833    0.000 depth_first_search.py:319(<genexpr>)
   761071    0.762    0.000    1.595    0.000 depth_first_search.py:322(dfs_labeled_edges)
       92    0.000    0.000    0.000    0.000 digraph.py:1032(is_multigraph)
      101    0.000    0.000    0.000    0.000 digraph.py:1036(is_directed)
       18    0.000    0.000    0.000    0.000 digraph.py:1040(fresh_copy)
       18    0.000    0.000    0.018    0.001 digraph.py:1054(copy)
     1997    0.001    0.000    0.001    0.000 digraph.py:1135(<genexpr>)
     4688    0.002    0.000    0.003    0.000 digraph.py:1136(<genexpr>)
       31    0.000    0.000    0.000    0.000 digraph.py:246(__init__)
  2248356    1.061    0.000    1.387    0.000 digraph.py:299(adj)
      396    0.000    0.000    0.000    0.000 digraph.py:318(succ)
      170    0.000    0.000    0.000    0.000 digraph.py:339(pred)
      396    0.000    0.000    0.000    0.000 digraph.py:355(add_node)
       29    0.005    0.000    0.006    0.000 digraph.py:401(add_nodes_from)
     2242    0.002    0.000    0.003    0.000 digraph.py:548(add_edge)
     2016    0.437    0.000    2.481    0.001 digraph.py:614(add_edges_from)
        9    0.002    0.000    0.002    0.000 digraph.py:709(remove_edges_from)
      226    0.000    0.000    0.001    0.000 digraph.py:775(edges)
       18    0.000    0.000    0.000    0.000 digraph.py:919(in_degree)
      170    0.041    0.000    0.353    0.002 edmondskarp.py:104(edmonds_karp_impl)
      170    0.000    0.000    0.353    0.002 edmondskarp.py:131(edmonds_karp)
      170    0.003    0.000    0.135    0.001 edmondskarp.py:17(edmonds_karp_core)
      421    0.012    0.000    0.022    0.000 edmondskarp.py:26(augment)
      494    0.037    0.000    0.109    0.000 edmondskarp.py:49(bidirectional_bfs)
        1    0.000    0.000    0.000    0.000 filters.py:28(hide_nodes)
      680    0.000    0.000    0.000    0.000 filters.py:30(<lambda>)
        1    0.000    0.000    0.000    0.000 filters.py:38(hide_edges)
        1    0.000    0.000    0.000    0.000 filters.py:39(<setcomp>)
      368    0.000    0.000    0.000    0.000 filters.py:40(<lambda>)
        1    0.000    0.000    0.000    0.000 function.py:435(restricted_view)
       10    0.000    0.000    0.001    0.000 function.py:589(set_node_attributes)
        1    0.000    0.000    0.000    0.000 function.py:67(number_of_nodes)
        1    0.000    0.000    0.000    0.000 function.py:678(get_node_attributes)
        1    0.000    0.000    0.000    0.000 function.py:700(<dictcomp>)
        1    0.000    0.000    0.000    0.000 function.py:72(number_of_edges)
        1    0.000    0.000    0.000    0.000 function.py:77(density)
      998    0.000    0.000    0.000    0.000 graph.py:1035(has_edge)
        2    0.000    0.000    0.000    0.000 graph.py:1076(neighbors)
       58    0.000    0.000    0.000    0.000 graph.py:1120(edges)
        3    0.000    0.000    0.000    0.000 graph.py:1243(degree)
        4    0.000    0.000    0.000    0.000 graph.py:1283(clear)
      165    0.000    0.000    0.000    0.000 graph.py:1302(is_multigraph)
       62    0.000    0.000    0.000    0.000 graph.py:1306(is_directed)
       58    0.000    0.000    0.000    0.000 graph.py:1310(fresh_copy)
       20    0.000    0.000    0.027    0.001 graph.py:1324(copy)
     2000    0.001    0.000    0.001    0.000 graph.py:1405(<genexpr>)
     8020    0.003    0.000    0.004    0.000 graph.py:1406(<genexpr>)
        1    0.000    0.000    0.000    0.000 graph.py:1623(size)
      100    0.000    0.000    0.000    0.000 graph.py:1659(<genexpr>)
        1    0.000    0.000    0.000    0.000 graph.py:1666(number_of_edges)
      197    0.000    0.000    0.000    0.000 graph.py:1718(nbunch_iter)
       69    0.000    0.000    0.000    0.000 graph.py:259(__init__)
       33    0.000    0.000    0.000    0.000 graph.py:306(adj)
        5    0.000    0.000    0.000    0.000 graph.py:335(name)
      355    0.000    0.000    0.000    0.000 graph.py:355(__iter__)
      734    0.000    0.000    0.000    0.000 graph.py:373(__contains__)
     2049    0.001    0.000    0.002    0.000 graph.py:387(__len__)
  2248366    1.645    0.000    4.318    0.000 graph.py:404(__getitem__)
       85    0.006    0.000    0.008    0.000 graph.py:475(add_nodes_from)
        6    0.000    0.000    0.000    0.000 graph.py:541(remove_node)
       20    0.000    0.000    0.000    0.000 graph.py:581(remove_nodes_from)
     2137    0.002    0.000    0.002    0.000 graph.py:616(nodes)
       26    0.000    0.000    0.000    0.000 graph.py:750(number_of_nodes)
        4    0.000    0.000    0.000    0.000 graph.py:770(order)
       46    0.000    0.000    0.000    0.000 graph.py:811(add_edge)
       82    0.021    0.000    0.038    0.000 graph.py:875(add_edges_from)
        1    0.000    0.000    0.000    0.000 graphviews.py:63(__init__)
       49    0.000    0.000    0.002    0.000 iostream.py:195(schedule)
       40    0.000    0.000    0.000    0.000 iostream.py:300(_is_master_process)
       40    0.000    0.000    0.000    0.000 iostream.py:313(_schedule_flush)
       40    0.000    0.000    0.002    0.000 iostream.py:366(write)
       49    0.000    0.000    0.000    0.000 iostream.py:93(_event_pipe)
        1    0.000    0.000    0.001    0.001 kcutsets.py:219(_is_separating_set)
        1    0.000    0.000    0.002    0.002 lattice.py:97(grid_graph)
       98    0.000    0.000    0.191    0.002 maxflow.py:175(maximum_flow_value)
       23    0.000    0.000    0.000    0.000 misc.py:199(is_iterator)
       23    0.000    0.000    0.000    0.000 misc.py:208(arbitrary_element)
        2    0.000    0.000    0.000    0.000 misc.py:244(pairwise)
       25    0.000    0.000    0.000    0.000 misc.py:63(is_string_like)
       75    0.000    0.000    0.000    0.000 misc.py:68(iterable)
       25    0.000    0.000    0.000    0.000 misc.py:79(flatten)
        1    0.000    0.000    0.000    0.000 product.py:118(_init_product_graph)
        1    0.000    0.000    0.001    0.001 product.py:186(cartesian_product)
       25    0.000    0.000    0.000    0.000 product.py:26(_dict_product)
       25    0.000    0.000    0.000    0.000 product.py:27(<genexpr>)
       26    0.000    0.000    0.000    0.000 product.py:31(_node_product)
       21    0.000    0.000    0.000    0.000 product.py:74(_edges_cross_nodes)
       21    0.000    0.000    0.000    0.000 product.py:88(_nodes_cross_edges)
       26    0.000    0.000    0.014    0.001 relabel.py:12(relabel_nodes)
       26    0.000    0.000    0.013    0.001 relabel.py:153(_relabel_copy)
      895    0.000    0.000    0.000    0.000 relabel.py:155(<genexpr>)
      895    0.001    0.000    0.001    0.000 relabel.py:156(<genexpr>)
     1646    0.002    0.000    0.004    0.000 relabel.py:161(<genexpr>)
       25    0.000    0.000    0.014    0.001 relabel.py:167(convert_node_labels_to_integers)
        1    0.000    0.000    0.000    0.000 relabel.py:223(<dictcomp>)
        1    0.000    0.000    0.000    0.000 relabel.py:93(<dictcomp>)
      402    0.000    0.000    0.000    0.000 reportviews.py:1025(__iter__)
     2137    0.001    0.000    0.001    0.000 reportviews.py:167(__init__)
       25    0.000    0.000    0.000    0.000 reportviews.py:171(__len__)
       56    0.000    0.000    0.000    0.000 reportviews.py:174(__iter__)
  1857160    0.283    0.000    0.283    0.000 reportviews.py:177(__getitem__)
       29    0.000    0.000    0.000    0.000 reportviews.py:189(__call__)
       21    0.000    0.000    0.000    0.000 reportviews.py:334(__init__)
       21    0.000    0.000    0.000    0.000 reportviews.py:342(__call__)
        1    0.000    0.000    0.000    0.000 reportviews.py:380(__len__)
      300    0.000    0.000    0.000    0.000 reportviews.py:442(__iter__)
      412    0.000    0.000    0.000    0.000 reportviews.py:489(__iter__)
      273    0.001    0.000    0.001    0.000 reportviews.py:642(__init__)
      197    0.000    0.000    0.000    0.000 reportviews.py:649(<lambda>)
    22434    0.002    0.000    0.002    0.000 reportviews.py:655(<lambda>)
      301    0.000    0.000    0.000    0.000 reportviews.py:657(<lambda>)
      217    0.000    0.000    0.000    0.000 reportviews.py:665(__iter__)
    19768    0.007    0.000    0.010    0.000 reportviews.py:666(<genexpr>)
     3240    0.003    0.000    0.004    0.000 reportviews.py:717(__iter__)
      284    0.000    0.000    0.001    0.000 reportviews.py:906(__init__)
     4378    0.001    0.000    0.001    0.000 reportviews.py:915(__iter__)
      284    0.001    0.000    0.002    0.000 reportviews.py:933(__call__)
        4    0.000    0.000    0.001    0.000 small.py:369(petersen_graph)
        4    0.000    0.000    0.001    0.000 small.py:48(make_small_undirected_graph)
        4    0.000    0.000    0.001    0.000 small.py:60(make_small_graph)
       49    0.001    0.000    0.001    0.000 socket.py:333(send)
      206    0.009    0.000    0.020    0.000 strongly_connected.py:27(strongly_connected_components)
        9    0.001    0.000    0.026    0.003 strongly_connected.py:361(condensation)
     1979    0.000    0.000    0.000    0.000 strongly_connected.py:411(<genexpr>)
      435    0.001    0.000    0.002    0.000 strongly_connected.py:414(<genexpr>)
       49    0.000    0.000    0.000    0.000 threading.py:573(isSet)
       49    0.000    0.000    0.000    0.000 threading.py:989(isAlive)
       40    0.000    0.000    0.000    0.000 utf_8.py:15(decode)
     1000    0.000    0.000    0.000    0.000 utils.py:119(<genexpr>)
        2    0.001    0.001    0.003    0.002 utils.py:13(build_auxiliary_node_connectivity)
        2    0.002    0.001    0.007    0.003 utils.py:73(build_residual_network)
       40    0.000    0.000    0.000    0.000 {_codecs.utf_8_decode}
    64921    0.017    0.000    0.017    0.000 {any}
       98    0.000    0.000    0.000    0.000 {callable}
      477    0.000    0.000    0.001    0.000 {hasattr}
       65    0.000    0.000    0.000    0.000 {isinstance}
        2    0.000    0.000    0.000    0.000 {itertools.tee}
500874/278268    0.117    0.000    0.184    0.000 {iter}
313594/311547    0.028    0.000    0.029    0.000 {len}
   224280    0.025    0.000    0.025    0.000 {method 'add' of 'set' objects}
       49    0.000    0.000    0.000    0.000 {method 'append' of 'collections.deque' objects}
  1003145    0.079    0.000    0.079    0.000 {method 'append' of 'list' objects}
       12    0.000    0.000    0.000    0.000 {method 'clear' of 'dict' objects}
    25453    0.002    0.000    0.002    0.000 {method 'copy' of 'dict' objects}
       40    0.000    0.000    0.000    0.000 {method 'decode' of 'str' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
   249646    0.031    0.000    0.031    0.000 {method 'get' of 'dict' objects}
   750782    0.142    0.000    0.142    0.000 {method 'issubset' of 'set' objects}
    10018    0.003    0.000    0.003    0.000 {method 'items' of 'dict' objects}
  1725914    0.234    0.000    0.234    0.000 {method 'pop' of 'list' objects}
      421    0.000    0.000    0.000    0.000 {method 'reverse' of 'list' objects}
   488096    0.093    0.000    0.094    0.000 {method 'update' of 'dict' objects}
 25776133   12.722    0.000   15.052    0.000 {method 'update' of 'set' objects}
     9463    0.002    0.000    0.002    0.000 {min}
   757980    0.089    0.000    0.089    0.000 {next}
       40    0.000    0.000    0.000    0.000 {posix.getpid}
       55    0.000    0.000    0.000    0.000 {range}
        1    0.000    0.000    0.000    0.000 {sorted}
       28    0.000    0.000    0.007    0.000 {sum}
       27    0.000    0.000    0.000    0.000 {zip}

@dschult dschult added this to the networkx-2.2 milestone Jul 24, 2018
@dschult

dschult commented Jul 24, 2018

Copy link
Copy Markdown
Member

Nice layout code for the comment! I didn't know you could hide details. :)

Yes, I agree that the next place to look for speedup is the antichains.
Also, none of this is helping with the complexity -- that will take more thought!

I think this is ready to merge. Anything else?

@wuhaochen

Copy link
Copy Markdown
Contributor Author

I'm checking to see if we can afford larger tests with the newer code, so maybe a bit later.

@wuhaochen

Copy link
Copy Markdown
Contributor Author

It seems the code works fine with edmonds_karp, but could be very slow with preflow_push when it finds a longer maximum flow and creating much more antichains, so we can test more cuts with the default flow algorithm but not the alternative flow.

We may want to see if using customized antichains can help, but I think it's ready to merge now.

@dschult
dschult merged commit 5fbd78e into networkx:master Jul 25, 2018
@wuhaochen
wuhaochen deleted the patch-all-node-cuts branch October 5, 2018 01:38
jtorrents added a commit to jtorrents/networkx that referenced this pull request Mar 26, 2026
…ithm.

The Even-Tarjan auxiliary digraph built by
build_auxiliary_node_connectivity sets capacity=1 on all edges. This is
correct for computing the node connectivity value, but Kanevsky's
algorithm depends on the structure of the residual graph, which requires
infinite capacity on external edges as the original Even-Tarjan
reduction specifies.

With unit-capacity external edges, these edges get saturated by the flow,
producing a residual graph with the wrong structure. The condensation DAG
has too many nodes, and the antichain count explodes exponentially.

This bug (introduced by me in the original NetworkX implementation) was
not identified when correctness bugs were reported in networkx#3025. The fix in
PR networkx#3039 addressed the symptoms (missing cuts) by adding compensating
filters: VE1 incident-node filtering, transitive closure on the full
residual graph, and internal-edge validation; and also introduced two
additional bugs:

- Removing backward arcs from the residual graph (capacity=0 edges with
  negative flow that represent cancellable flow and must be preserved)
- Building closed sets using predecessors instead of successors in the
  condensation DAG, producing the wrong side of the cut partition

These filters discarded most of the millions of spurious antichains
generated by the broken condensation DAG, making results mostly correct
but at enormous computational cost.

This fix addresses the root cause and restores the algorithm to match the
paper's specification:
- Set infinite capacity on external edges before building the residual
- Remove only edges with zero residual capacity (capacity == flow)
- Build successor-closed sets from antichins of the condensation DAG
- Find cut edges from S to ~S using H successors.

The compensating code is no longer needed and has been removed.

Performance on grid_2d_graph(11,11) improves from 180s to 0.16s (n=121)
(1000x+), and now scales polynomially as predicted by the paper. Results
verified identical to igraph's C implementation at all tested sizes.

Fixes networkx#7994.

Tests previously marked @pytest.mark.slow are now fast enough to run by
default, and MAX_CUTSETS_TO_TEST is restored from 4 to 100.
jtorrents added a commit to jtorrents/networkx that referenced this pull request Apr 1, 2026
…ithm.

The Even-Tarjan auxiliary digraph built by
build_auxiliary_node_connectivity sets capacity=1 on all edges. This is
correct for computing the node connectivity value, but Kanevsky's
algorithm depends on the structure of the residual graph, which requires
infinite capacity on external edges as the original Even-Tarjan
reduction specifies.

With unit-capacity external edges, these edges get saturated by the flow,
producing a residual graph with the wrong structure. The condensation DAG
has too many nodes, and the antichain count explodes exponentially.

This bug (introduced by me in the original NetworkX implementation) was
not identified when correctness bugs were reported in networkx#3025. The fix in
PR networkx#3039 addressed the symptoms (missing cuts) by adding compensating
filters: VE1 incident-node filtering, transitive closure on the full
residual graph, and internal-edge validation; and also introduced two
additional bugs:

- Removing backward arcs from the residual graph (capacity=0 edges with
  negative flow that represent cancellable flow and must be preserved)
- Building closed sets using predecessors instead of successors in the
  condensation DAG, producing the wrong side of the cut partition

These filters discarded most of the millions of spurious antichains
generated by the broken condensation DAG, making results mostly correct
but at enormous computational cost.

This fix addresses the root cause and restores the algorithm to match the
paper's specification:
- Set infinite capacity on external edges before building the residual
- Remove only edges with zero residual capacity (capacity == flow)
- Build successor-closed sets from antichins of the condensation DAG
- Find cut edges from S to ~S using H successors.

The compensating code is no longer needed and has been removed.

Performance on grid_2d_graph(11,11) improves from 180s to 0.16s (n=121)
(1000x+), and now scales polynomially as predicted by the paper. Results
verified identical to igraph's C implementation at all tested sizes.

Fixes networkx#7994.

Tests previously marked @pytest.mark.slow are now fast enough to run by
default, and MAX_CUTSETS_TO_TEST is restored from 4 to 100.
dschult pushed a commit that referenced this pull request Apr 10, 2026
…ithm (#8586)

Fix all_node_cuts: correct Even-Tarjan reduction for Kanevsky's algorithm.

The Even-Tarjan auxiliary digraph built by
build_auxiliary_node_connectivity sets capacity=1 on all edges. This is
correct for computing the node connectivity value, but Kanevsky's
algorithm depends on the structure of the residual graph, which requires
infinite capacity on external edges as the original Even-Tarjan
reduction specifies.

With unit-capacity external edges, these edges get saturated by the flow,
producing a residual graph with the wrong structure. The condensation DAG
has too many nodes, and the antichain count explodes exponentially.

This bug (introduced by me in the original NetworkX implementation) was
not identified when correctness bugs were reported in #3025. The fix in
PR #3039 addressed the symptoms (missing cuts) by adding compensating
filters: VE1 incident-node filtering, transitive closure on the full
residual graph, and internal-edge validation; and also introduced two
additional bugs:

- Removing backward arcs from the residual graph (capacity=0 edges with
  negative flow that represent cancellable flow and must be preserved)
- Building closed sets using predecessors instead of successors in the
  condensation DAG, producing the wrong side of the cut partition

These filters discarded most of the millions of spurious antichains
generated by the broken condensation DAG, making results mostly correct
but at enormous computational cost.

This fix addresses the root cause and restores the algorithm to match the
paper's specification:
- Set infinite capacity on external edges before building the residual
- Remove only edges with zero residual capacity (capacity == flow)
- Build successor-closed sets from antichins of the condensation DAG
- Find cut edges from S to ~S using H successors.

The compensating code is no longer needed and has been removed.

Performance on grid_2d_graph(11,11) improves from 180s to 0.16s (n=121)
(1000x+), and now scales polynomially as predicted by the paper. Results
verified identical to igraph's C implementation at all tested sizes.

Fixes #7994.

Tests previously marked @pytest.mark.slow are now fast enough to run by
default, and MAX_CUTSETS_TO_TEST is restored from 4 to 100.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants