Skip to content

BUG: Fix node connectivity and node cuts for digraphs - #8837

Merged
rossbar merged 2 commits into
networkx:mainfrom
jtorrents:digraph-connectivity-fixes
Aug 21, 2026
Merged

rossbar merged 2 commits into
networkx:mainfrom
jtorrents:digraph-connectivity-fixes

Conversation

@jtorrents

Copy link
Copy Markdown
Member

node_connectivity, minimum_node_cut and minimum_st_node_cut return wrong results on directed graphs. node_connectivity is also wrong on graphs with self-loops or parallel edges, and minimum_node_cut on graphs with self-loops. minimum_node_cut can return a set that is not a cut at all.

As an illustration of the bugs:

>>> import networkx as nx

>>> # a directed triangle has node connectivity 1
>>> nx.minimum_node_cut(nx.DiGraph([(0, 1), (1, 2), (2, 0)]))
set()

>>> # weakly but not strongly connected, so the node connectivity is 0
>>> nx.node_connectivity(nx.DiGraph([(0, 3), (1, 2), (2, 1), (3, 0), (3, 1), (3, 2)]))
1

>>> # K5 has node connectivity 4, self-loops should not change that
>>> G = nx.complete_graph(5)
>>> G.add_edges_from((u, u) for u in G)
>>> nx.node_connectivity(G)
6

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:

  1. 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.

  2. Initial cutset from successors only. minimum_node_cut used set(G[v]), which does not isolate a node that also has predecessors and is empty for a node without successors.

  3. Duplicated neighbors. Predecessors and successors were chained without deduplication, so a node that is both was yielded twice and paired with itself.

  4. minimum_st_node_cut checked on both arc directions. It returned the empty set when G.has_edge(s, t) or G.has_edge(t, s). An arc from t to s is not an s-t path and does not stop s from being separated from t. minimum_node_cut then accepted that empty set as a smaller cut.

  5. 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:

In case G is a directed graph, similar definitions and approach lead to the
same result, except that for each v1, v2, ..., vk, we compute both
N(vi, v) and N(v, vi)
(which are now not necessarily the same) for each
applicable v.

and its footnote 7, which is exactly the rule in bug 4:

If there is an edge from vi to v, N(vi, v) is not computed, and if there is an
edge from v to vi, N(v, vi) is not computed.

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_cut changes behavior on digraphs. Where the pair is joined only by an arc from target to source, it now returns a real cut instead of set(). Undirected graphs are unaffected.

  • minimum_node_cut may 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.

`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.
@dschult

dschult commented Aug 11, 2026

Copy link
Copy Markdown
Member

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?

@jtorrents

Copy link
Copy Markdown
Member Author

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: min(predecessors, successors), never the union.

What I meant. A node cut of a digraph is a set of nodes whose removal makes the graph not strongly connected. So v is separated from the rest as soon as it either cannot reach the rest (remove all its successors) or cannot be reached from the rest (remove all its predecessors). Either side alone does it, which is why the bound is the smaller of the two.

That is bug 1 (node_connectivity needs the size of that set, as the initial K) and bug 2 (minimum_node_cut needs the set itself, as the initial min_cut). Same statement, two functions. The old code gave two different wrong answers to the same question: node_connectivity used G.degree (in + out, which over-estimates the bound) and minimum_node_cut used set(G[v]) (the successor side, unconditionally).

Where I was sloppy. I wrote that set(G[v]) "does not isolate a node that also has predecessors". That is not the case, and it is what made the two bugs look contradictory. Removing all successors of v leaves v a sink, so it is a perfectly valid cut. The one exception is a self-loop: set(G[v]) then contains v itself, and a set containing v is not a cut for v, which is why the fix excludes it with - {v}. Leaving self-loops aside, the actual defect is that set(G[v]) doesn't have to be the smallest isolating set, so minimum_node_cut can return a cut that is not minimum:

>>> 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 {0} and {1, 3} isolate node 2, but {0} is the smallest and what we should return. The old code starts from set(G[2]) == {1, 3} and no later pair beats it, so it returns a cut of size 2 for a graph whose node connectivity is 1.

@dschult dschult left a comment

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.

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 rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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!

@rossbar
rossbar merged commit 416c3e2 into networkx:main Aug 21, 2026
43 checks passed
@dschult dschult added this to the 3.7 milestone Aug 21, 2026
@jtorrents

Copy link
Copy Markdown
Member Author

Great! Thanks @dschult and @rossbar for the review!

jtorrents added a commit to jtorrents/networkx that referenced this pull request Oct 25, 2026
Requested in review. The deterministic digraph, self-loop and multigraph
tests cover the fix, as in the exact function's suite since networkx#8837.
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.

3 participants