Skip to content

Commit

Permalink
Fix all_node_cuts output for complete graphs (networkx#6558)
Browse files Browse the repository at this point in the history
* Fix for complete graphs case in all_node_cuts

* Check node_connectivty in complete graph

* test for broken cuts. add fix

---------

Co-authored-by: Dan Schult <dschult@colgate.edu>
  • Loading branch information
2 people authored and cvanelteren committed Apr 22, 2024
1 parent 1d3aa1d commit 04769fb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
7 changes: 4 additions & 3 deletions networkx/algorithms/connectivity/kcutsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ def all_node_cuts(G, k=None, flow_func=None):

# Address some corner cases first.
# For complete Graphs

if nx.density(G) == 1:
for cut_set in combinations(G, len(G) - 1):
yield set(cut_set)
yield from ()
return

# Initialize data structures.
# Keep track of the cuts already computed so we do not repeat them.
seen = []
Expand Down Expand Up @@ -130,7 +131,7 @@ def all_node_cuts(G, k=None, flow_func=None):
for x in X:
# step 3: Compute local connectivity flow of x with all other
# non adjacent nodes in G
non_adjacent = set(G) - X - set(G[x])
non_adjacent = set(G) - {x} - set(G[x])
for v in non_adjacent:
# step 4: compute maximum flow in an Even-Tarjan reduction H of G
# and step 5: build the associated residual network R
Expand Down
17 changes: 12 additions & 5 deletions networkx/algorithms/connectivity/tests/test_kcutsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,15 @@ def test_cycle_graph():

def test_complete_graph():
G = nx.complete_graph(5)
solution = [{0, 1, 2, 3}, {0, 1, 2, 4}, {0, 1, 3, 4}, {0, 2, 3, 4}, {1, 2, 3, 4}]
cuts = list(nx.all_node_cuts(G))
assert len(solution) == len(cuts)
for cut in cuts:
assert cut in solution
assert nx.node_connectivity(G) == 4
assert list(nx.all_node_cuts(G)) == []


def test_all_node_cuts_simple_case():
G = nx.complete_graph(5)
G.remove_edges_from([(0, 1), (3, 4)])
expected = [{0, 1, 2}, {2, 3, 4}]
actual = list(nx.all_node_cuts(G))
assert len(actual) == len(expected)
for cut in actual:
assert cut in expected

0 comments on commit 04769fb

Please sign in to comment.