boykovkolmogorov.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. """
  2. Boykov-Kolmogorov algorithm for maximum flow problems.
  3. """
  4. from collections import deque
  5. from operator import itemgetter
  6. import networkx as nx
  7. from networkx.algorithms.flow.utils import build_residual_network
  8. __all__ = ["boykov_kolmogorov"]
  9. def boykov_kolmogorov(
  10. G, s, t, capacity="capacity", residual=None, value_only=False, cutoff=None
  11. ):
  12. r"""Find a maximum single-commodity flow using Boykov-Kolmogorov algorithm.
  13. This function returns the residual network resulting after computing
  14. the maximum flow. See below for details about the conventions
  15. NetworkX uses for defining residual networks.
  16. This algorithm has worse case complexity $O(n^2 m |C|)$ for $n$ nodes, $m$
  17. edges, and $|C|$ the cost of the minimum cut [1]_. This implementation
  18. uses the marking heuristic defined in [2]_ which improves its running
  19. time in many practical problems.
  20. Parameters
  21. ----------
  22. G : NetworkX graph
  23. Edges of the graph are expected to have an attribute called
  24. 'capacity'. If this attribute is not present, the edge is
  25. considered to have infinite capacity.
  26. s : node
  27. Source node for the flow.
  28. t : node
  29. Sink node for the flow.
  30. capacity : string
  31. Edges of the graph G are expected to have an attribute capacity
  32. that indicates how much flow the edge can support. If this
  33. attribute is not present, the edge is considered to have
  34. infinite capacity. Default value: 'capacity'.
  35. residual : NetworkX graph
  36. Residual network on which the algorithm is to be executed. If None, a
  37. new residual network is created. Default value: None.
  38. value_only : bool
  39. If True compute only the value of the maximum flow. This parameter
  40. will be ignored by this algorithm because it is not applicable.
  41. cutoff : integer, float
  42. If specified, the algorithm will terminate when the flow value reaches
  43. or exceeds the cutoff. In this case, it may be unable to immediately
  44. determine a minimum cut. Default value: None.
  45. Returns
  46. -------
  47. R : NetworkX DiGraph
  48. Residual network after computing the maximum flow.
  49. Raises
  50. ------
  51. NetworkXError
  52. The algorithm does not support MultiGraph and MultiDiGraph. If
  53. the input graph is an instance of one of these two classes, a
  54. NetworkXError is raised.
  55. NetworkXUnbounded
  56. If the graph has a path of infinite capacity, the value of a
  57. feasible flow on the graph is unbounded above and the function
  58. raises a NetworkXUnbounded.
  59. See also
  60. --------
  61. :meth:`maximum_flow`
  62. :meth:`minimum_cut`
  63. :meth:`preflow_push`
  64. :meth:`shortest_augmenting_path`
  65. Notes
  66. -----
  67. The residual network :samp:`R` from an input graph :samp:`G` has the
  68. same nodes as :samp:`G`. :samp:`R` is a DiGraph that contains a pair
  69. of edges :samp:`(u, v)` and :samp:`(v, u)` iff :samp:`(u, v)` is not a
  70. self-loop, and at least one of :samp:`(u, v)` and :samp:`(v, u)` exists
  71. in :samp:`G`.
  72. For each edge :samp:`(u, v)` in :samp:`R`, :samp:`R[u][v]['capacity']`
  73. is equal to the capacity of :samp:`(u, v)` in :samp:`G` if it exists
  74. in :samp:`G` or zero otherwise. If the capacity is infinite,
  75. :samp:`R[u][v]['capacity']` will have a high arbitrary finite value
  76. that does not affect the solution of the problem. This value is stored in
  77. :samp:`R.graph['inf']`. For each edge :samp:`(u, v)` in :samp:`R`,
  78. :samp:`R[u][v]['flow']` represents the flow function of :samp:`(u, v)` and
  79. satisfies :samp:`R[u][v]['flow'] == -R[v][u]['flow']`.
  80. The flow value, defined as the total flow into :samp:`t`, the sink, is
  81. stored in :samp:`R.graph['flow_value']`. If :samp:`cutoff` is not
  82. specified, reachability to :samp:`t` using only edges :samp:`(u, v)` such
  83. that :samp:`R[u][v]['flow'] < R[u][v]['capacity']` induces a minimum
  84. :samp:`s`-:samp:`t` cut.
  85. Examples
  86. --------
  87. >>> from networkx.algorithms.flow import boykov_kolmogorov
  88. The functions that implement flow algorithms and output a residual
  89. network, such as this one, are not imported to the base NetworkX
  90. namespace, so you have to explicitly import them from the flow package.
  91. >>> G = nx.DiGraph()
  92. >>> G.add_edge("x", "a", capacity=3.0)
  93. >>> G.add_edge("x", "b", capacity=1.0)
  94. >>> G.add_edge("a", "c", capacity=3.0)
  95. >>> G.add_edge("b", "c", capacity=5.0)
  96. >>> G.add_edge("b", "d", capacity=4.0)
  97. >>> G.add_edge("d", "e", capacity=2.0)
  98. >>> G.add_edge("c", "y", capacity=2.0)
  99. >>> G.add_edge("e", "y", capacity=3.0)
  100. >>> R = boykov_kolmogorov(G, "x", "y")
  101. >>> flow_value = nx.maximum_flow_value(G, "x", "y")
  102. >>> flow_value
  103. 3.0
  104. >>> flow_value == R.graph["flow_value"]
  105. True
  106. A nice feature of the Boykov-Kolmogorov algorithm is that a partition
  107. of the nodes that defines a minimum cut can be easily computed based
  108. on the search trees used during the algorithm. These trees are stored
  109. in the graph attribute `trees` of the residual network.
  110. >>> source_tree, target_tree = R.graph["trees"]
  111. >>> partition = (set(source_tree), set(G) - set(source_tree))
  112. Or equivalently:
  113. >>> partition = (set(G) - set(target_tree), set(target_tree))
  114. References
  115. ----------
  116. .. [1] Boykov, Y., & Kolmogorov, V. (2004). An experimental comparison
  117. of min-cut/max-flow algorithms for energy minimization in vision.
  118. Pattern Analysis and Machine Intelligence, IEEE Transactions on,
  119. 26(9), 1124-1137.
  120. https://doi.org/10.1109/TPAMI.2004.60
  121. .. [2] Vladimir Kolmogorov. Graph-based Algorithms for Multi-camera
  122. Reconstruction Problem. PhD thesis, Cornell University, CS Department,
  123. 2003. pp. 109-114.
  124. https://web.archive.org/web/20170809091249/https://pub.ist.ac.at/~vnk/papers/thesis.pdf
  125. """
  126. R = boykov_kolmogorov_impl(G, s, t, capacity, residual, cutoff)
  127. R.graph["algorithm"] = "boykov_kolmogorov"
  128. return R
  129. def boykov_kolmogorov_impl(G, s, t, capacity, residual, cutoff):
  130. if s not in G:
  131. raise nx.NetworkXError(f"node {str(s)} not in graph")
  132. if t not in G:
  133. raise nx.NetworkXError(f"node {str(t)} not in graph")
  134. if s == t:
  135. raise nx.NetworkXError("source and sink are the same node")
  136. if residual is None:
  137. R = build_residual_network(G, capacity)
  138. else:
  139. R = residual
  140. # Initialize/reset the residual network.
  141. # This is way too slow
  142. # nx.set_edge_attributes(R, 0, 'flow')
  143. for u in R:
  144. for e in R[u].values():
  145. e["flow"] = 0
  146. # Use an arbitrary high value as infinite. It is computed
  147. # when building the residual network.
  148. INF = R.graph["inf"]
  149. if cutoff is None:
  150. cutoff = INF
  151. R_succ = R.succ
  152. R_pred = R.pred
  153. def grow():
  154. """Bidirectional breadth-first search for the growth stage.
  155. Returns a connecting edge, that is and edge that connects
  156. a node from the source search tree with a node from the
  157. target search tree.
  158. The first node in the connecting edge is always from the
  159. source tree and the last node from the target tree.
  160. """
  161. while active:
  162. u = active[0]
  163. if u in source_tree:
  164. this_tree = source_tree
  165. other_tree = target_tree
  166. neighbors = R_succ
  167. else:
  168. this_tree = target_tree
  169. other_tree = source_tree
  170. neighbors = R_pred
  171. for v, attr in neighbors[u].items():
  172. if attr["capacity"] - attr["flow"] > 0:
  173. if v not in this_tree:
  174. if v in other_tree:
  175. return (u, v) if this_tree is source_tree else (v, u)
  176. this_tree[v] = u
  177. dist[v] = dist[u] + 1
  178. timestamp[v] = timestamp[u]
  179. active.append(v)
  180. elif v in this_tree and _is_closer(u, v):
  181. this_tree[v] = u
  182. dist[v] = dist[u] + 1
  183. timestamp[v] = timestamp[u]
  184. _ = active.popleft()
  185. return None, None
  186. def augment(u, v):
  187. """Augmentation stage.
  188. Reconstruct path and determine its residual capacity.
  189. We start from a connecting edge, which links a node
  190. from the source tree to a node from the target tree.
  191. The connecting edge is the output of the grow function
  192. and the input of this function.
  193. """
  194. attr = R_succ[u][v]
  195. flow = min(INF, attr["capacity"] - attr["flow"])
  196. path = [u]
  197. # Trace a path from u to s in source_tree.
  198. w = u
  199. while w != s:
  200. n = w
  201. w = source_tree[n]
  202. attr = R_pred[n][w]
  203. flow = min(flow, attr["capacity"] - attr["flow"])
  204. path.append(w)
  205. path.reverse()
  206. # Trace a path from v to t in target_tree.
  207. path.append(v)
  208. w = v
  209. while w != t:
  210. n = w
  211. w = target_tree[n]
  212. attr = R_succ[n][w]
  213. flow = min(flow, attr["capacity"] - attr["flow"])
  214. path.append(w)
  215. # Augment flow along the path and check for saturated edges.
  216. it = iter(path)
  217. u = next(it)
  218. these_orphans = []
  219. for v in it:
  220. R_succ[u][v]["flow"] += flow
  221. R_succ[v][u]["flow"] -= flow
  222. if R_succ[u][v]["flow"] == R_succ[u][v]["capacity"]:
  223. if v in source_tree:
  224. source_tree[v] = None
  225. these_orphans.append(v)
  226. if u in target_tree:
  227. target_tree[u] = None
  228. these_orphans.append(u)
  229. u = v
  230. orphans.extend(sorted(these_orphans, key=dist.get))
  231. return flow
  232. def adopt():
  233. """Adoption stage.
  234. Reconstruct search trees by adopting or discarding orphans.
  235. During augmentation stage some edges got saturated and thus
  236. the source and target search trees broke down to forests, with
  237. orphans as roots of some of its trees. We have to reconstruct
  238. the search trees rooted to source and target before we can grow
  239. them again.
  240. """
  241. while orphans:
  242. u = orphans.popleft()
  243. if u in source_tree:
  244. tree = source_tree
  245. neighbors = R_pred
  246. else:
  247. tree = target_tree
  248. neighbors = R_succ
  249. nbrs = ((n, attr, dist[n]) for n, attr in neighbors[u].items() if n in tree)
  250. for v, attr, d in sorted(nbrs, key=itemgetter(2)):
  251. if attr["capacity"] - attr["flow"] > 0:
  252. if _has_valid_root(v, tree):
  253. tree[u] = v
  254. dist[u] = dist[v] + 1
  255. timestamp[u] = time
  256. break
  257. else:
  258. nbrs = (
  259. (n, attr, dist[n]) for n, attr in neighbors[u].items() if n in tree
  260. )
  261. for v, attr, d in sorted(nbrs, key=itemgetter(2)):
  262. if attr["capacity"] - attr["flow"] > 0:
  263. if v not in active:
  264. active.append(v)
  265. if tree[v] == u:
  266. tree[v] = None
  267. orphans.appendleft(v)
  268. if u in active:
  269. active.remove(u)
  270. del tree[u]
  271. def _has_valid_root(n, tree):
  272. path = []
  273. v = n
  274. while v is not None:
  275. path.append(v)
  276. if v in (s, t):
  277. base_dist = 0
  278. break
  279. elif timestamp[v] == time:
  280. base_dist = dist[v]
  281. break
  282. v = tree[v]
  283. else:
  284. return False
  285. length = len(path)
  286. for i, u in enumerate(path, 1):
  287. dist[u] = base_dist + length - i
  288. timestamp[u] = time
  289. return True
  290. def _is_closer(u, v):
  291. return timestamp[v] <= timestamp[u] and dist[v] > dist[u] + 1
  292. source_tree = {s: None}
  293. target_tree = {t: None}
  294. active = deque([s, t])
  295. orphans = deque()
  296. flow_value = 0
  297. # data structures for the marking heuristic
  298. time = 1
  299. timestamp = {s: time, t: time}
  300. dist = {s: 0, t: 0}
  301. while flow_value < cutoff:
  302. # Growth stage
  303. u, v = grow()
  304. if u is None:
  305. break
  306. time += 1
  307. # Augmentation stage
  308. flow_value += augment(u, v)
  309. # Adoption stage
  310. adopt()
  311. if flow_value * 2 > INF:
  312. raise nx.NetworkXUnbounded("Infinite capacity path, flow unbounded above.")
  313. # Add source and target tree in a graph attribute.
  314. # A partition that defines a minimum cut can be directly
  315. # computed from the search trees as explained in the docstrings.
  316. R.graph["trees"] = (source_tree, target_tree)
  317. # Add the standard flow_value graph attribute.
  318. R.graph["flow_value"] = flow_value
  319. return R