Repository navigation
BUG: Fix node connectivity and node cuts for digraphs - #8837
Conversation
`node_connectivity` and `minimum_node_cut` implement algorithm 11 of Esfahanian's Connectivity Algorithms, which is stated for undirected graphs only. Its adaptation to digraphs was wrong in several ways, so both returned values that are too large, and `minimum_node_cut` could return a set that is not a cut at all. Five distinct defects: 1. The starting node was chosen by total degree. Node connectivity of a digraph is bounded by the smaller of the in-degree and the out-degree, since isolating a node only requires removing all its predecessors or all its successors. 2. `minimum_node_cut` built its initial cutset from `set(G[v])`, the successors of the starting node alone. That does not isolate a node which also has predecessors, and it is empty for a node without successors, so the empty set could be returned as a minimum node cut. 3. The pairs of neighbors were built by chaining predecessors and successors, so a node that is both was yielded twice and paired with itself. 4. `minimum_st_node_cut` returned the empty set when either of the two arcs between `s` and `t` was present. An arc from `t` to `s` is not an `s`-`t` path and does not stop `s` from being separated from `t`. The empty set was then taken for a smaller cut by `minimum_node_cut`. 5. The starting node was only ever used as the source. Node connectivity of a digraph is a minimum over ordered pairs and the two orders need not agree, so when the starting node lay on the target side of every minimum cut the smallest value was never computed. Page 513 of Even and Tarjan, Network Flow and Testing Graph Connectivity, SIAM J. Comput. 4(4):507-518, 1975, states that for a digraph both N(v, w) and N(w, v) have to be computed, and its footnote 7 gives the per-direction rule of defect 4. Self-loops and parallel edges also inflated the initial bound, in undirected graphs too. They are irrelevant for connectivity, so `G.degree` cannot be used to pick the starting node. On a complete graph no pair of nodes is left to lower the inflated bound again, and it was returned unchanged.
|
Thanks -- nice work visiting rabbits! :) I have a question about bugs 1 and 2. Bug 1 suggests that a node is isolated when either all preds or all succs are removed. I would have thought "isolated" means it isn't connected in both in and out directions. Then bug 2 seems to say that to make a node isolated we have to ensure that both successors and predecessors need to be cut. The two seem to contradict each other in the definition of isolated. What am I missing? |
|
Hi Dan! Thanks for reading it carefully; you are not missing anything, my wording in bug 2 is just wrong (but not the code in the fix). Both bugs use the same notion of "isolated", and the fix for both is the same: What I meant. A node cut of a digraph is a set of nodes whose removal makes the graph not strongly connected. So That is bug 1 ( Where I was sloppy. I wrote that >>> G = nx.DiGraph([(0, 1), (0, 2), (0, 3), (1, 0), (1, 3), (2, 1), (2, 3), (3, 0), (3, 1)])
>>> nx.is_strongly_connected(G)
True
>>> nx.node_connectivity(G) # with this PR
1
>>> set(G.pred[2]), set(G.succ[2]) # node 2 is the minimum degree node
({0}, {1, 3})Both |
dschult
left a comment
There was a problem hiding this comment.
The comments here are really helpful and make this easier to review. Thank you!
I don't see any improvements. This all looks good to me!
Since this fixes bugs for DiGraphs in these routines, let's try to get this in for v3.7.
rossbar
left a comment
There was a problem hiding this comment.
LGTM, thanks @jtorrents . I took the liberty of changing the word "arc" -> "edge" in the docs/comments but the changes themselves look good and I agree the comments are helpful!
Requested in review. The deterministic digraph, self-loop and multigraph tests cover the fix, as in the exact function's suite since networkx#8837.
node_connectivity,minimum_node_cutandminimum_st_node_cutreturn wrong results on directed graphs.node_connectivityis also wrong on graphs with self-loops or parallel edges, andminimum_node_cuton graphs with self-loops.minimum_node_cutcan return a set that is not a cut at all.As an illustration of the bugs:
All three functions implement algorithm 11 of Esfahanian's Connectivity Algorithms (cited in the docstrings), which is from Esfahanian and Hakimi (Networks 14:355-366, 1984). That algorithm is stated for undirected graphs only; section 4 of the chapter and section III of the 1984 paper never mention digraphs. When I implemented this for NetworkX in 2012 it seems that I just derived the implementation for digraphs, and introduced 5 bugs in doing so:
Starting node chosen by total degree. Node connectivity of a digraph is bounded by the smaller of the in-degree and the out-degree, since isolating a node only requires removing all its predecessors or all its successors.
Initial cutset from successors only.
minimum_node_cutusedset(G[v]), which does not isolate a node that also has predecessors and is empty for a node without successors.Duplicated neighbors. Predecessors and successors were chained without deduplication, so a node that is both was yielded twice and paired with itself.
minimum_st_node_cutchecked on both arc directions. It returned the empty set whenG.has_edge(s, t) or G.has_edge(t, s). An arc fromttosis not ans-tpath and does not stopsfrom being separated fromt.minimum_node_cutthen accepted that empty set as a smaller cut.Starting node only ever used as the source. Node connectivity of a digraph is a minimum over ordered pairs and the two orders need not agree, so when the starting node lay on the target side of every minimum cut the smallest value was never computed.
Bugs 1 and 3 also affect self-loops and parallel edges: they inflate the initial bound, and on a complete graph no pair of nodes is left to lower it again, so the inflated value is returned.
In order to fix bug 5 I checked the literature and found that Even and Tarjan, Network Flow and Testing Graph Connectivity (SIAM J. Comput. 4(4):507-518, 1975), p. 513 say:
and its footnote 7, which is exactly the rule in bug 4:
Even and Tarjan paper is now cited in the docstrings of all three functions.
These fixes have an impact on public functions:
minimum_st_node_cutchanges behavior on digraphs. Where the pair is joined only by an arc from target to source, it now returns a real cut instead ofset(). Undirected graphs are unaffected.minimum_node_cutmay also return a different minimum cut than before on some digraphs. It is still a minimum cut and the concrete cut returned was already dependent on iteration order.I found all these bugs while working on a pre-filter proposed for node_connectivity by Sinkovits (ICCS 2021) which provides a 2.5x-52x speedup while being also exact. Some tests for digraphs failed and that got me into this rabbit hole.