本文整理汇总了Python中networkx.minimum_cut方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.minimum_cut方法的具体用法?Python networkx.minimum_cut怎么用?Python networkx.minimum_cut使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.minimum_cut方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_constrs
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import minimum_cut [as 别名]
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
示例2: generate_constrs
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import minimum_cut [as 别名]
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
示例3: generate_constrs
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import minimum_cut [as 别名]
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
示例4: hypothesis_errors
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import minimum_cut [as 别名]
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)
示例5: get_single_mask
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import minimum_cut [as 别名]
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
示例6: generate_constrs
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import minimum_cut [as 别名]
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)
示例7: compare_flows_and_cuts
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import minimum_cut [as 别名]
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)
示例8: test_minimum_cut_no_cutoff
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import minimum_cut [as 别名]
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)