maxflow.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. """
  2. Maximum flow (and minimum cut) algorithms on capacitated graphs.
  3. """
  4. import networkx as nx
  5. from .boykovkolmogorov import boykov_kolmogorov
  6. from .dinitz_alg import dinitz
  7. from .edmondskarp import edmonds_karp
  8. from .preflowpush import preflow_push
  9. from .shortestaugmentingpath import shortest_augmenting_path
  10. from .utils import build_flow_dict
  11. # Define the default flow function for computing maximum flow.
  12. default_flow_func = preflow_push
  13. __all__ = ["maximum_flow", "maximum_flow_value", "minimum_cut", "minimum_cut_value"]
  14. @nx._dispatchable(graphs="flowG", edge_attrs={"capacity": float("inf")})
  15. def maximum_flow(flowG, _s, _t, capacity="capacity", flow_func=None, **kwargs):
  16. """Find a maximum single-commodity flow.
  17. Parameters
  18. ----------
  19. flowG : NetworkX graph
  20. Edges of the graph are expected to have an attribute called
  21. 'capacity'. If this attribute is not present, the edge is
  22. considered to have infinite capacity.
  23. _s : node
  24. Source node for the flow.
  25. _t : node
  26. Sink node for the flow.
  27. capacity : string
  28. Edges of the graph G are expected to have an attribute capacity
  29. that indicates how much flow the edge can support. If this
  30. attribute is not present, the edge is considered to have
  31. infinite capacity. Default value: 'capacity'.
  32. flow_func : function
  33. A function for computing the maximum flow among a pair of nodes
  34. in a capacitated graph. The function has to accept at least three
  35. parameters: a Graph or Digraph, a source node, and a target node.
  36. And return a residual network that follows NetworkX conventions
  37. (see Notes). If flow_func is None, the default maximum
  38. flow function (:meth:`preflow_push`) is used. See below for
  39. alternative algorithms. The choice of the default function may change
  40. from version to version and should not be relied on. Default value:
  41. None.
  42. kwargs : Any other keyword parameter is passed to the function that
  43. computes the maximum flow.
  44. Returns
  45. -------
  46. flow_value : integer, float
  47. Value of the maximum flow, i.e., net outflow from the source.
  48. flow_dict : dict
  49. A dictionary containing the value of the flow that went through
  50. each edge.
  51. Raises
  52. ------
  53. NetworkXError
  54. The algorithm does not support MultiGraph and MultiDiGraph. If
  55. the input graph is an instance of one of these two classes, a
  56. NetworkXError is raised.
  57. NetworkXUnbounded
  58. If the graph has a path of infinite capacity, the value of a
  59. feasible flow on the graph is unbounded above and the function
  60. raises a NetworkXUnbounded.
  61. See also
  62. --------
  63. :meth:`maximum_flow_value`
  64. :meth:`minimum_cut`
  65. :meth:`minimum_cut_value`
  66. :meth:`edmonds_karp`
  67. :meth:`preflow_push`
  68. :meth:`shortest_augmenting_path`
  69. Notes
  70. -----
  71. The function used in the flow_func parameter has to return a residual
  72. network that follows NetworkX conventions:
  73. The residual network :samp:`R` from an input graph :samp:`G` has the
  74. same nodes as :samp:`G`. :samp:`R` is a DiGraph that contains a pair
  75. of edges :samp:`(u, v)` and :samp:`(v, u)` iff :samp:`(u, v)` is not a
  76. self-loop, and at least one of :samp:`(u, v)` and :samp:`(v, u)` exists
  77. in :samp:`G`.
  78. For each edge :samp:`(u, v)` in :samp:`R`, :samp:`R[u][v]['capacity']`
  79. is equal to the capacity of :samp:`(u, v)` in :samp:`G` if it exists
  80. in :samp:`G` or zero otherwise. If the capacity is infinite,
  81. :samp:`R[u][v]['capacity']` will have a high arbitrary finite value
  82. that does not affect the solution of the problem. This value is stored in
  83. :samp:`R.graph['inf']`. For each edge :samp:`(u, v)` in :samp:`R`,
  84. :samp:`R[u][v]['flow']` represents the flow function of :samp:`(u, v)` and
  85. satisfies :samp:`R[u][v]['flow'] == -R[v][u]['flow']`.
  86. The flow value, defined as the total flow into :samp:`t`, the sink, is
  87. stored in :samp:`R.graph['flow_value']`. Reachability to :samp:`t` using
  88. only edges :samp:`(u, v)` such that
  89. :samp:`R[u][v]['flow'] < R[u][v]['capacity']` induces a minimum
  90. :samp:`s`-:samp:`t` cut.
  91. Specific algorithms may store extra data in :samp:`R`.
  92. The function should supports an optional boolean parameter value_only. When
  93. True, it can optionally terminate the algorithm as soon as the maximum flow
  94. value and the minimum cut can be determined.
  95. Note that the resulting maximum flow may contain flow cycles,
  96. back-flow to the source, or some flow exiting the sink.
  97. These are possible if there are cycles in the network.
  98. Examples
  99. --------
  100. >>> G = nx.DiGraph()
  101. >>> G.add_edge("x", "a", capacity=3.0)
  102. >>> G.add_edge("x", "b", capacity=1.0)
  103. >>> G.add_edge("a", "c", capacity=3.0)
  104. >>> G.add_edge("b", "c", capacity=5.0)
  105. >>> G.add_edge("b", "d", capacity=4.0)
  106. >>> G.add_edge("d", "e", capacity=2.0)
  107. >>> G.add_edge("c", "y", capacity=2.0)
  108. >>> G.add_edge("e", "y", capacity=3.0)
  109. maximum_flow returns both the value of the maximum flow and a
  110. dictionary with all flows.
  111. >>> flow_value, flow_dict = nx.maximum_flow(G, "x", "y")
  112. >>> flow_value
  113. 3.0
  114. >>> print(flow_dict["x"]["b"])
  115. 1.0
  116. You can also use alternative algorithms for computing the
  117. maximum flow by using the flow_func parameter.
  118. >>> from networkx.algorithms.flow import shortest_augmenting_path
  119. >>> flow_value == nx.maximum_flow(G, "x", "y", flow_func=shortest_augmenting_path)[
  120. ... 0
  121. ... ]
  122. True
  123. """
  124. if flow_func is None:
  125. if kwargs:
  126. raise nx.NetworkXError(
  127. "You have to explicitly set a flow_func if"
  128. " you need to pass parameters via kwargs."
  129. )
  130. flow_func = default_flow_func
  131. if not callable(flow_func):
  132. raise nx.NetworkXError("flow_func has to be callable.")
  133. R = flow_func(flowG, _s, _t, capacity=capacity, value_only=False, **kwargs)
  134. flow_dict = build_flow_dict(flowG, R)
  135. return (R.graph["flow_value"], flow_dict)
  136. @nx._dispatchable(graphs="flowG", edge_attrs={"capacity": float("inf")})
  137. def maximum_flow_value(flowG, _s, _t, capacity="capacity", flow_func=None, **kwargs):
  138. """Find the value of maximum single-commodity flow.
  139. Parameters
  140. ----------
  141. flowG : NetworkX graph
  142. Edges of the graph are expected to have an attribute called
  143. 'capacity'. If this attribute is not present, the edge is
  144. considered to have infinite capacity.
  145. _s : node
  146. Source node for the flow.
  147. _t : node
  148. Sink node for the flow.
  149. capacity : string
  150. Edges of the graph G are expected to have an attribute capacity
  151. that indicates how much flow the edge can support. If this
  152. attribute is not present, the edge is considered to have
  153. infinite capacity. Default value: 'capacity'.
  154. flow_func : function
  155. A function for computing the maximum flow among a pair of nodes
  156. in a capacitated graph. The function has to accept at least three
  157. parameters: a Graph or Digraph, a source node, and a target node.
  158. And return a residual network that follows NetworkX conventions
  159. (see Notes). If flow_func is None, the default maximum
  160. flow function (:meth:`preflow_push`) is used. See below for
  161. alternative algorithms. The choice of the default function may change
  162. from version to version and should not be relied on. Default value:
  163. None.
  164. kwargs : Any other keyword parameter is passed to the function that
  165. computes the maximum flow.
  166. Returns
  167. -------
  168. flow_value : integer, float
  169. Value of the maximum flow, i.e., net outflow from the source.
  170. Raises
  171. ------
  172. NetworkXError
  173. The algorithm does not support MultiGraph and MultiDiGraph. If
  174. the input graph is an instance of one of these two classes, a
  175. NetworkXError is raised.
  176. NetworkXUnbounded
  177. If the graph has a path of infinite capacity, the value of a
  178. feasible flow on the graph is unbounded above and the function
  179. raises a NetworkXUnbounded.
  180. See also
  181. --------
  182. :meth:`maximum_flow`
  183. :meth:`minimum_cut`
  184. :meth:`minimum_cut_value`
  185. :meth:`edmonds_karp`
  186. :meth:`preflow_push`
  187. :meth:`shortest_augmenting_path`
  188. Notes
  189. -----
  190. The function used in the flow_func parameter has to return a residual
  191. network that follows NetworkX conventions:
  192. The residual network :samp:`R` from an input graph :samp:`G` has the
  193. same nodes as :samp:`G`. :samp:`R` is a DiGraph that contains a pair
  194. of edges :samp:`(u, v)` and :samp:`(v, u)` iff :samp:`(u, v)` is not a
  195. self-loop, and at least one of :samp:`(u, v)` and :samp:`(v, u)` exists
  196. in :samp:`G`.
  197. For each edge :samp:`(u, v)` in :samp:`R`, :samp:`R[u][v]['capacity']`
  198. is equal to the capacity of :samp:`(u, v)` in :samp:`G` if it exists
  199. in :samp:`G` or zero otherwise. If the capacity is infinite,
  200. :samp:`R[u][v]['capacity']` will have a high arbitrary finite value
  201. that does not affect the solution of the problem. This value is stored in
  202. :samp:`R.graph['inf']`. For each edge :samp:`(u, v)` in :samp:`R`,
  203. :samp:`R[u][v]['flow']` represents the flow function of :samp:`(u, v)` and
  204. satisfies :samp:`R[u][v]['flow'] == -R[v][u]['flow']`.
  205. The flow value, defined as the total flow into :samp:`t`, the sink, is
  206. stored in :samp:`R.graph['flow_value']`. Reachability to :samp:`t` using
  207. only edges :samp:`(u, v)` such that
  208. :samp:`R[u][v]['flow'] < R[u][v]['capacity']` induces a minimum
  209. :samp:`s`-:samp:`t` cut.
  210. Specific algorithms may store extra data in :samp:`R`.
  211. The function should supports an optional boolean parameter value_only. When
  212. True, it can optionally terminate the algorithm as soon as the maximum flow
  213. value and the minimum cut can be determined.
  214. Examples
  215. --------
  216. >>> G = nx.DiGraph()
  217. >>> G.add_edge("x", "a", capacity=3.0)
  218. >>> G.add_edge("x", "b", capacity=1.0)
  219. >>> G.add_edge("a", "c", capacity=3.0)
  220. >>> G.add_edge("b", "c", capacity=5.0)
  221. >>> G.add_edge("b", "d", capacity=4.0)
  222. >>> G.add_edge("d", "e", capacity=2.0)
  223. >>> G.add_edge("c", "y", capacity=2.0)
  224. >>> G.add_edge("e", "y", capacity=3.0)
  225. maximum_flow_value computes only the value of the
  226. maximum flow:
  227. >>> flow_value = nx.maximum_flow_value(G, "x", "y")
  228. >>> flow_value
  229. 3.0
  230. You can also use alternative algorithms for computing the
  231. maximum flow by using the flow_func parameter.
  232. >>> from networkx.algorithms.flow import shortest_augmenting_path
  233. >>> flow_value == nx.maximum_flow_value(
  234. ... G, "x", "y", flow_func=shortest_augmenting_path
  235. ... )
  236. True
  237. """
  238. if flow_func is None:
  239. if kwargs:
  240. raise nx.NetworkXError(
  241. "You have to explicitly set a flow_func if"
  242. " you need to pass parameters via kwargs."
  243. )
  244. flow_func = default_flow_func
  245. if not callable(flow_func):
  246. raise nx.NetworkXError("flow_func has to be callable.")
  247. R = flow_func(flowG, _s, _t, capacity=capacity, value_only=True, **kwargs)
  248. return R.graph["flow_value"]
  249. @nx._dispatchable(graphs="flowG", edge_attrs={"capacity": float("inf")})
  250. def minimum_cut(flowG, _s, _t, capacity="capacity", flow_func=None, **kwargs):
  251. """Compute the value and the node partition of a minimum (s, t)-cut.
  252. Use the max-flow min-cut theorem, i.e., the capacity of a minimum
  253. capacity cut is equal to the flow value of a maximum flow.
  254. Parameters
  255. ----------
  256. flowG : NetworkX graph
  257. Edges of the graph are expected to have an attribute called
  258. 'capacity'. If this attribute is not present, the edge is
  259. considered to have infinite capacity.
  260. _s : node
  261. Source node for the flow.
  262. _t : node
  263. Sink node for the flow.
  264. capacity : string
  265. Edges of the graph G are expected to have an attribute capacity
  266. that indicates how much flow the edge can support. If this
  267. attribute is not present, the edge is considered to have
  268. infinite capacity. Default value: 'capacity'.
  269. flow_func : function
  270. A function for computing the maximum flow among a pair of nodes
  271. in a capacitated graph. The function has to accept at least three
  272. parameters: a Graph or Digraph, a source node, and a target node.
  273. And return a residual network that follows NetworkX conventions
  274. (see Notes). If flow_func is None, the default maximum
  275. flow function (:meth:`preflow_push`) is used. See below for
  276. alternative algorithms. The choice of the default function may change
  277. from version to version and should not be relied on. Default value:
  278. None.
  279. kwargs : Any other keyword parameter is passed to the function that
  280. computes the maximum flow.
  281. Returns
  282. -------
  283. cut_value : integer, float
  284. Value of the minimum cut.
  285. partition : pair of node sets
  286. A partitioning of the nodes that defines a minimum cut.
  287. Raises
  288. ------
  289. NetworkXUnbounded
  290. If the graph has a path of infinite capacity, all cuts have
  291. infinite capacity and the function raises a NetworkXError.
  292. See also
  293. --------
  294. :meth:`maximum_flow`
  295. :meth:`maximum_flow_value`
  296. :meth:`minimum_cut_value`
  297. :meth:`edmonds_karp`
  298. :meth:`preflow_push`
  299. :meth:`shortest_augmenting_path`
  300. Notes
  301. -----
  302. The function used in the flow_func parameter has to return a residual
  303. network that follows NetworkX conventions:
  304. The residual network :samp:`R` from an input graph :samp:`G` has the
  305. same nodes as :samp:`G`. :samp:`R` is a DiGraph that contains a pair
  306. of edges :samp:`(u, v)` and :samp:`(v, u)` iff :samp:`(u, v)` is not a
  307. self-loop, and at least one of :samp:`(u, v)` and :samp:`(v, u)` exists
  308. in :samp:`G`.
  309. For each edge :samp:`(u, v)` in :samp:`R`, :samp:`R[u][v]['capacity']`
  310. is equal to the capacity of :samp:`(u, v)` in :samp:`G` if it exists
  311. in :samp:`G` or zero otherwise. If the capacity is infinite,
  312. :samp:`R[u][v]['capacity']` will have a high arbitrary finite value
  313. that does not affect the solution of the problem. This value is stored in
  314. :samp:`R.graph['inf']`. For each edge :samp:`(u, v)` in :samp:`R`,
  315. :samp:`R[u][v]['flow']` represents the flow function of :samp:`(u, v)` and
  316. satisfies :samp:`R[u][v]['flow'] == -R[v][u]['flow']`.
  317. The flow value, defined as the total flow into :samp:`t`, the sink, is
  318. stored in :samp:`R.graph['flow_value']`. Reachability to :samp:`t` using
  319. only edges :samp:`(u, v)` such that
  320. :samp:`R[u][v]['flow'] < R[u][v]['capacity']` induces a minimum
  321. :samp:`s`-:samp:`t` cut.
  322. Specific algorithms may store extra data in :samp:`R`.
  323. The function should supports an optional boolean parameter value_only. When
  324. True, it can optionally terminate the algorithm as soon as the maximum flow
  325. value and the minimum cut can be determined.
  326. Examples
  327. --------
  328. >>> G = nx.DiGraph()
  329. >>> G.add_edge("x", "a", capacity=3.0)
  330. >>> G.add_edge("x", "b", capacity=1.0)
  331. >>> G.add_edge("a", "c", capacity=3.0)
  332. >>> G.add_edge("b", "c", capacity=5.0)
  333. >>> G.add_edge("b", "d", capacity=4.0)
  334. >>> G.add_edge("d", "e", capacity=2.0)
  335. >>> G.add_edge("c", "y", capacity=2.0)
  336. >>> G.add_edge("e", "y", capacity=3.0)
  337. minimum_cut computes both the value of the
  338. minimum cut and the node partition:
  339. >>> cut_value, partition = nx.minimum_cut(G, "x", "y")
  340. >>> reachable, non_reachable = partition
  341. 'partition' here is a tuple with the two sets of nodes that define
  342. the minimum cut. You can compute the cut set of edges that induce
  343. the minimum cut as follows:
  344. >>> cutset = set()
  345. >>> for u, nbrs in ((n, G[n]) for n in reachable):
  346. ... cutset.update((u, v) for v in nbrs if v in non_reachable)
  347. >>> print(sorted(cutset))
  348. [('c', 'y'), ('x', 'b')]
  349. >>> cut_value == sum(G.edges[u, v]["capacity"] for (u, v) in cutset)
  350. True
  351. You can also use alternative algorithms for computing the
  352. minimum cut by using the flow_func parameter.
  353. >>> from networkx.algorithms.flow import shortest_augmenting_path
  354. >>> cut_value == nx.minimum_cut(G, "x", "y", flow_func=shortest_augmenting_path)[0]
  355. True
  356. """
  357. if flow_func is None:
  358. if kwargs:
  359. raise nx.NetworkXError(
  360. "You have to explicitly set a flow_func if"
  361. " you need to pass parameters via kwargs."
  362. )
  363. flow_func = default_flow_func
  364. if not callable(flow_func):
  365. raise nx.NetworkXError("flow_func has to be callable.")
  366. if kwargs.get("cutoff") is not None and flow_func is preflow_push:
  367. raise nx.NetworkXError("cutoff should not be specified.")
  368. R = flow_func(flowG, _s, _t, capacity=capacity, value_only=True, **kwargs)
  369. # Remove saturated edges from the residual network
  370. cutset = [(u, v, d) for u, v, d in R.edges(data=True) if d["flow"] == d["capacity"]]
  371. R.remove_edges_from(cutset)
  372. # Then, reachable and non reachable nodes from source in the
  373. # residual network form the node partition that defines
  374. # the minimum cut.
  375. non_reachable = set(nx.shortest_path_length(R, target=_t))
  376. partition = (set(flowG) - non_reachable, non_reachable)
  377. # Finally add again cutset edges to the residual network to make
  378. # sure that it is reusable.
  379. R.add_edges_from(cutset)
  380. return (R.graph["flow_value"], partition)
  381. @nx._dispatchable(graphs="flowG", edge_attrs={"capacity": float("inf")})
  382. def minimum_cut_value(flowG, _s, _t, capacity="capacity", flow_func=None, **kwargs):
  383. """Compute the value of a minimum (s, t)-cut.
  384. Use the max-flow min-cut theorem, i.e., the capacity of a minimum
  385. capacity cut is equal to the flow value of a maximum flow.
  386. Parameters
  387. ----------
  388. flowG : NetworkX graph
  389. Edges of the graph are expected to have an attribute called
  390. 'capacity'. If this attribute is not present, the edge is
  391. considered to have infinite capacity.
  392. _s : node
  393. Source node for the flow.
  394. _t : node
  395. Sink node for the flow.
  396. capacity : string
  397. Edges of the graph G are expected to have an attribute capacity
  398. that indicates how much flow the edge can support. If this
  399. attribute is not present, the edge is considered to have
  400. infinite capacity. Default value: 'capacity'.
  401. flow_func : function
  402. A function for computing the maximum flow among a pair of nodes
  403. in a capacitated graph. The function has to accept at least three
  404. parameters: a Graph or Digraph, a source node, and a target node.
  405. And return a residual network that follows NetworkX conventions
  406. (see Notes). If flow_func is None, the default maximum
  407. flow function (:meth:`preflow_push`) is used. See below for
  408. alternative algorithms. The choice of the default function may change
  409. from version to version and should not be relied on. Default value:
  410. None.
  411. kwargs : Any other keyword parameter is passed to the function that
  412. computes the maximum flow.
  413. Returns
  414. -------
  415. cut_value : integer, float
  416. Value of the minimum cut.
  417. Raises
  418. ------
  419. NetworkXUnbounded
  420. If the graph has a path of infinite capacity, all cuts have
  421. infinite capacity and the function raises a NetworkXError.
  422. See also
  423. --------
  424. :meth:`maximum_flow`
  425. :meth:`maximum_flow_value`
  426. :meth:`minimum_cut`
  427. :meth:`edmonds_karp`
  428. :meth:`preflow_push`
  429. :meth:`shortest_augmenting_path`
  430. Notes
  431. -----
  432. The function used in the flow_func parameter has to return a residual
  433. network that follows NetworkX conventions:
  434. The residual network :samp:`R` from an input graph :samp:`G` has the
  435. same nodes as :samp:`G`. :samp:`R` is a DiGraph that contains a pair
  436. of edges :samp:`(u, v)` and :samp:`(v, u)` iff :samp:`(u, v)` is not a
  437. self-loop, and at least one of :samp:`(u, v)` and :samp:`(v, u)` exists
  438. in :samp:`G`.
  439. For each edge :samp:`(u, v)` in :samp:`R`, :samp:`R[u][v]['capacity']`
  440. is equal to the capacity of :samp:`(u, v)` in :samp:`G` if it exists
  441. in :samp:`G` or zero otherwise. If the capacity is infinite,
  442. :samp:`R[u][v]['capacity']` will have a high arbitrary finite value
  443. that does not affect the solution of the problem. This value is stored in
  444. :samp:`R.graph['inf']`. For each edge :samp:`(u, v)` in :samp:`R`,
  445. :samp:`R[u][v]['flow']` represents the flow function of :samp:`(u, v)` and
  446. satisfies :samp:`R[u][v]['flow'] == -R[v][u]['flow']`.
  447. The flow value, defined as the total flow into :samp:`t`, the sink, is
  448. stored in :samp:`R.graph['flow_value']`. Reachability to :samp:`t` using
  449. only edges :samp:`(u, v)` such that
  450. :samp:`R[u][v]['flow'] < R[u][v]['capacity']` induces a minimum
  451. :samp:`s`-:samp:`t` cut.
  452. Specific algorithms may store extra data in :samp:`R`.
  453. The function should supports an optional boolean parameter value_only. When
  454. True, it can optionally terminate the algorithm as soon as the maximum flow
  455. value and the minimum cut can be determined.
  456. Examples
  457. --------
  458. >>> G = nx.DiGraph()
  459. >>> G.add_edge("x", "a", capacity=3.0)
  460. >>> G.add_edge("x", "b", capacity=1.0)
  461. >>> G.add_edge("a", "c", capacity=3.0)
  462. >>> G.add_edge("b", "c", capacity=5.0)
  463. >>> G.add_edge("b", "d", capacity=4.0)
  464. >>> G.add_edge("d", "e", capacity=2.0)
  465. >>> G.add_edge("c", "y", capacity=2.0)
  466. >>> G.add_edge("e", "y", capacity=3.0)
  467. minimum_cut_value computes only the value of the
  468. minimum cut:
  469. >>> cut_value = nx.minimum_cut_value(G, "x", "y")
  470. >>> cut_value
  471. 3.0
  472. You can also use alternative algorithms for computing the
  473. minimum cut by using the flow_func parameter.
  474. >>> from networkx.algorithms.flow import shortest_augmenting_path
  475. >>> cut_value == nx.minimum_cut_value(
  476. ... G, "x", "y", flow_func=shortest_augmenting_path
  477. ... )
  478. True
  479. """
  480. if flow_func is None:
  481. if kwargs:
  482. raise nx.NetworkXError(
  483. "You have to explicitly set a flow_func if"
  484. " you need to pass parameters via kwargs."
  485. )
  486. flow_func = default_flow_func
  487. if not callable(flow_func):
  488. raise nx.NetworkXError("flow_func has to be callable.")
  489. if kwargs.get("cutoff") is not None and flow_func is preflow_push:
  490. raise nx.NetworkXError("cutoff should not be specified.")
  491. R = flow_func(flowG, _s, _t, capacity=capacity, value_only=True, **kwargs)
  492. return R.graph["flow_value"]