Skip to content

Commit 6912188

Browse files
atomassiAndrea Tomassilli
authored andcommitted
fixes solution
1 parent 25d22e7 commit 6912188

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

networkx/algorithms/approximation/kcutsets.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,13 @@ def minimum_multiway_cut(G, terminals, weight=None):
105105
G2.add_edge(u, sink, capacity=float("inf"))
106106

107107
# discard the heaviest cut and take the union of the rest
108-
cutset = set(
109-
itertools.chain.from_iterable(
110-
el[0] for el in sorted(all_cuts, key=lambda x: x[1])[:-1]
111-
)
112-
)
108+
cutset = set()
109+
for u, v in itertools.chain.from_iterable(
110+
el[0] for el in sorted(all_cuts, key=lambda x: x[1])[:-1]
111+
):
112+
if (u, v) not in cutset and (v, u) not in cutset:
113+
cutset.add((u, v))
114+
113115
# compute the cut value
114116
cut_value = sum(G2.edges[u, v]["capacity"] for u, v in cutset)
115117

@@ -160,7 +162,7 @@ def minimum_k_cut(G, k, weight=None):
160162
Value of the (approximated) minimum k-cut.
161163
162164
cutset : set
163-
Set of edges whose removal leaves k connected components.
165+
Set of edges whose removal leaves at least k connected components.
164166
165167
Raises
166168
------
@@ -206,17 +208,22 @@ def minimum_k_cut(G, k, weight=None):
206208
min_weight_edges = sorted(T.edges(data="weight"), key=lambda x: x[2])[: k - 1]
207209

208210
# compute the cutset, the value is computed after as an edge might appear more than once
209-
cutset = set()
211+
all_edges_cut = set()
210212
for u, v, _ in min_weight_edges:
211213
# remove (u,v) from the tree
212214
T.remove_edge(u, v)
213215
# get the connected component that contains u
214216
p1 = nx.node_connected_component(T, u)
215217
# add the boundary edges of p1 to the cutset
216-
cutset |= set(nx.edge_boundary(G2, p1))
218+
all_edges_cut |= set(nx.edge_boundary(G2, p1))
217219
# re-add (u,v) to the tree
218220
T.add_edge(u, v)
219221

222+
# consider edges only in a direction
223+
cutset = set()
224+
for u, v in all_edges_cut:
225+
if (u, v) not in cutset and (v, u) not in cutset:
226+
cutset.add((u, v))
220227
# compute the cut_value
221228
cut_value = sum(G2.edges[u, v]["capacity"] for u, v in cutset)
222229

0 commit comments

Comments
 (0)