Python networkx.minimum_cut() Examples
The following are 12
code examples of networkx.minimum_cut().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
networkx
, or try the search function
.
Example #1
Source File: tsp-cuts-ulysses22.py From python-mip with Eclipse Public License 2.0 | 6 votes |
def generate_constrs(self, m_: Model): xf, cp, Gl = m_.translate(self.x), CutPool(), nx.DiGraph() Ar = [(i, j) for (i, j) in Arcs if xf[i][j] and xf[i][j].x >= 1e-4] for (u, v) in Ar: Gl.add_edge(u, v, capacity=xf[u][v].x) for (u, v) in F: val, (S, NS) = nx.minimum_cut(Gl, u, v) if val <= 0.99: aInS = [(xf[i][j], xf[i][j].x) for (i, j) in Ar if i in S and j in S] if sum(f for v, f in aInS) >= (len(S) - 1) + 1e-4: cut = xsum(1.0 * v for v, fm in aInS) <= len(S) - 1 cp.add(cut) if len(cp.cuts) > 32: for cut in cp.cuts: m_ += cut return for cut in cp.cuts: m_ += cut
Example #2
Source File: tsp-cuts.py From python-mip with Eclipse Public License 2.0 | 6 votes |
def generate_constrs(self, model: Model): xf, V_, cp, G = model.translate(self.x), self.V, CutPool(), nx.DiGraph() for (u, v) in [(k, l) for (k, l) in product(V_, V_) if k != l and xf[k][l]]: G.add_edge(u, v, capacity=xf[u][v].x) for (u, v) in F: val, (S, NS) = nx.minimum_cut(G, u, v) if val <= 0.99: aInS = [(xf[i][j], xf[i][j].x) for (i, j) in product(V_, V_) if i != j and xf[i][j] and i in S and j in S] if sum(f for v, f in aInS) >= (len(S)-1)+1e-4: cut = xsum(1.0*v for v, fm in aInS) <= len(S)-1 cp.add(cut) if len(cp.cuts) > 256: for cut in cp.cuts: model += cut return for cut in cp.cuts: model += cut
Example #3
Source File: tsp.py From python-mip with Eclipse Public License 2.0 | 6 votes |
def generate_constrs(self, model: Model): G = nx.DiGraph() r = [(v, v.x) for v in model.vars if v.name.startswith('x(')] U = [int(v.name.split('(')[1].split(',')[0]) for v, f in r] V = [int(v.name.split(')')[0].split(',')[1]) for v, f in r] cp = CutPool() for i in range(len(U)): G.add_edge(U[i], V[i], capacity=r[i][1]) for (u, v) in F: if u not in U or v not in V: continue val, (S, NS) = nx.minimum_cut(G, u, v) if val <= 0.99: arcsInS = [(v, f) for i, (v, f) in enumerate(r) if U[i] in S and V[i] in S] if sum(f for v, f in arcsInS) >= (len(S)-1)+1e-4: cut = xsum(1.0*v for v, fm in arcsInS) <= len(S)-1 cp.add(cut) if len(cp.cuts) > 256: for cut in cp.cuts: model += cut return for cut in cp.cuts: model += cut return
Example #4
Source File: mixin_dynamic.py From ibeis with Apache License 2.0 | 5 votes |
def hypothesis_errors(infr, pos_subgraph, neg_edges): if not nx.is_connected(pos_subgraph): raise AssertionError('Not connected' + repr(pos_subgraph)) infr.print( 'Find hypothesis errors in {} nodes with {} neg edges'.format( len(pos_subgraph), len(neg_edges)), 3) pos_edges = list(pos_subgraph.edges()) neg_weight = infr._mincut_edge_weights(neg_edges) pos_weight = infr._mincut_edge_weights(pos_edges) capacity = 'weight' nx.set_edge_attributes(pos_subgraph, name=capacity, values=ut.dzip(pos_edges, pos_weight)) # Solve a multicut problem for multiple pairs of terminal nodes. # Running multiple min-cuts produces a k-factor approximation maybe_error_edges = set([]) for (s, t), join_weight in zip(neg_edges, neg_weight): cut_weight, parts = nx.minimum_cut(pos_subgraph, s, t, capacity=capacity) cut_edgeset = nxu.edges_cross(pos_subgraph, *parts) if join_weight < cut_weight: join_edgeset = {(s, t)} chosen = join_edgeset hypothesis = POSTV else: chosen = cut_edgeset hypothesis = NEGTV for edge in chosen: if edge not in maybe_error_edges: maybe_error_edges.add(edge) yield (edge, hypothesis)
Example #5
Source File: graphcut.py From dual-fisheye-video-stitching with MIT License | 5 votes |
def get_single_mask(imgA_t, imgB_t, imgA_t_1=None, imgB_t_1=None, smask_old=None): h, w = imgA_t.shape[:2] L2_NORM_t = np.linalg.norm(imgA_t - imgB_t, axis=2) if smask_old is None: G = build_graph(h, w, [L2_NORM_t]) cut_value, partition = nx.minimum_cut(G, str(h * w), str(h * w + 1)) else: L2_NORM_AA = np.linalg.norm(imgA_t - imgA_t_1, axis=2) L2_NORM_BB = np.linalg.norm(imgB_t - imgB_t_1, axis=2) L2_NORM_AB = np.linalg.norm(imgA_t - imgB_t_1, axis=2) L2_NORM_BA = np.linalg.norm(imgB_t - imgA_t_1, axis=2) G = build_graph(h, w, [L2_NORM_t, L2_NORM_AA + L2_NORM_BB + L2_NORM_AB + L2_NORM_BA], smask_old) cut_value, partition = nx.minimum_cut( G, str(2 * h * w), str(2 * h * w + 1)) reachable, non_reachable = partition mask = np.zeros((h * w,)) reachable = np.int32(list(reachable)) mask[reachable[reachable < h * w]] = 1 mask = mask.reshape((h, w)) mask_color = np.zeros((h, w, 3)) mask_color[:, :, 0] = mask mask_color[:, :, 1] = mask mask_color[:, :, 2] = mask return mask_color
Example #6
Source File: mip_test.py From python-mip with Eclipse Public License 2.0 | 5 votes |
def generate_constrs(self, model: Model): G = nx.DiGraph() r = [(v, v.x) for v in model.vars if v.name.startswith("x(")] U = [v.name.split("(")[1].split(",")[0] for v, f in r] V = [v.name.split(")")[0].split(",")[1] for v, f in r] N = list(set(U + V)) cp = CutPool() for i in range(len(U)): G.add_edge(U[i], V[i], capacity=r[i][1]) for (u, v) in product(N, N): if u == v: continue val, (S, NS) = nx.minimum_cut(G, u, v) if val <= 0.99: arcsInS = [ (v, f) for i, (v, f) in enumerate(r) if U[i] in S and V[i] in S ] if sum(f for v, f in arcsInS) >= (len(S) - 1) + 1e-4: cut = xsum(1.0 * v for v, fm in arcsInS) <= len(S) - 1 cp.add(cut) if len(cp.cuts) > 256: for cut in cp.cuts: model.add_cut(cut) return for cut in cp.cuts: model.add_cut(cut)
Example #7
Source File: test_maxflow.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def compare_flows_and_cuts(G, s, t, solnFlows, solnValue, capacity='capacity'): for flow_func in flow_funcs: R = flow_func(G, s, t, capacity) # Test both legacy and new implementations. flow_value = R.graph['flow_value'] flow_dict = build_flow_dict(G, R) assert_equal(flow_value, solnValue, msg=msg.format(flow_func.__name__)) validate_flows(G, s, t, flow_dict, solnValue, capacity, flow_func) # Minimum cut cut_value, partition = nx.minimum_cut(G, s, t, capacity=capacity, flow_func=flow_func) validate_cuts(G, s, t, solnValue, partition, capacity, flow_func)
Example #8
Source File: test_maxflow.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_minimum_cut_no_cutoff(self): G = self.G for flow_func in flow_funcs: assert_raises(nx.NetworkXError, nx.minimum_cut, G, 'x', 'y', flow_func=flow_func, cutoff=1.0) assert_raises(nx.NetworkXError, nx.minimum_cut_value, G, 'x', 'y', flow_func=flow_func, cutoff=1.0)
Example #9
Source File: test_maxflow.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def compare_flows_and_cuts(G, s, t, solnFlows, solnValue, capacity='capacity'): for flow_func in flow_funcs: R = flow_func(G, s, t, capacity) # Test both legacy and new implementations. flow_value = R.graph['flow_value'] flow_dict = build_flow_dict(G, R) assert_equal(flow_value, solnValue, msg=msg.format(flow_func.__name__)) validate_flows(G, s, t, flow_dict, solnValue, capacity, flow_func) # Minimum cut cut_value, partition = nx.minimum_cut(G, s, t, capacity=capacity, flow_func=flow_func) validate_cuts(G, s, t, solnValue, partition, capacity, flow_func)
Example #10
Source File: test_maxflow.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_minimum_cut_no_cutoff(self): G = self.G for flow_func in flow_funcs: assert_raises(nx.NetworkXError, nx.minimum_cut, G, 'x', 'y', flow_func=flow_func, cutoff=1.0) assert_raises(nx.NetworkXError, nx.minimum_cut_value, G, 'x', 'y', flow_func=flow_func, cutoff=1.0)
Example #11
Source File: test_maxflow.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def compare_flows_and_cuts(G, s, t, solnFlows, solnValue, capacity='capacity'): for flow_func in flow_funcs: R = flow_func(G, s, t, capacity) # Test both legacy and new implementations. flow_value = R.graph['flow_value'] flow_dict = build_flow_dict(G, R) assert_equal(flow_value, solnValue, msg=msg.format(flow_func.__name__)) validate_flows(G, s, t, flow_dict, solnValue, capacity, flow_func) # Minimum cut cut_value, partition = nx.minimum_cut(G, s, t, capacity=capacity, flow_func=flow_func) validate_cuts(G, s, t, solnValue, partition, capacity, flow_func)
Example #12
Source File: test_maxflow.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_minimum_cut_no_cutoff(self): G = self.G for flow_func in flow_funcs: assert_raises(nx.NetworkXError, nx.minimum_cut, G, 'x', 'y', flow_func=flow_func, cutoff=1.0) assert_raises(nx.NetworkXError, nx.minimum_cut_value, G, 'x', 'y', flow_func=flow_func, cutoff=1.0)