<ahref="max__flow_8h.html">Go to the documentation of this file.</a><divclass="fragment"><divclass="line"><aid="l00001"name="l00001"></a><spanclass="lineno"> 1</span><spanclass="comment">// Copyright 2010-2021 Google LLC</span></div>
<divclass="line"><aid="l00002"name="l00002"></a><spanclass="lineno"> 2</span><spanclass="comment">// Licensed under the Apache License, Version 2.0 (the "License");</span></div>
<divclass="line"><aid="l00003"name="l00003"></a><spanclass="lineno"> 3</span><spanclass="comment">// you may not use this file except in compliance with the License.</span></div>
<divclass="line"><aid="l00004"name="l00004"></a><spanclass="lineno"> 4</span><spanclass="comment">// You may obtain a copy of the License at</span></div>
<divclass="line"><aid="l00008"name="l00008"></a><spanclass="lineno"> 8</span><spanclass="comment">// Unless required by applicable law or agreed to in writing, software</span></div>
<divclass="line"><aid="l00009"name="l00009"></a><spanclass="lineno"> 9</span><spanclass="comment">// distributed under the License is distributed on an "AS IS" BASIS,</span></div>
<divclass="line"><aid="l00010"name="l00010"></a><spanclass="lineno"> 10</span><spanclass="comment">// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></div>
<divclass="line"><aid="l00011"name="l00011"></a><spanclass="lineno"> 11</span><spanclass="comment">// See the License for the specific language governing permissions and</span></div>
<divclass="line"><aid="l00012"name="l00012"></a><spanclass="lineno"> 12</span><spanclass="comment">// limitations under the License.</span></div>
<divclass="line"><aid="l00014"name="l00014"></a><spanclass="lineno"> 14</span><spanclass="comment">// An implementation of a push-relabel algorithm for the max flow problem.</span></div>
<divclass="line"><aid="l00016"name="l00016"></a><spanclass="lineno"> 16</span><spanclass="comment">// In the following, we consider a graph G = (V,E,s,t) where V denotes the set</span></div>
<divclass="line"><aid="l00017"name="l00017"></a><spanclass="lineno"> 17</span><spanclass="comment">// of nodes (vertices) in the graph, E denotes the set of arcs (edges). s and t</span></div>
<divclass="line"><aid="l00018"name="l00018"></a><spanclass="lineno"> 18</span><spanclass="comment">// denote distinguished nodes in G called source and target. n = |V| denotes the</span></div>
<divclass="line"><aid="l00019"name="l00019"></a><spanclass="lineno"> 19</span><spanclass="comment">// number of nodes in the graph, and m = |E| denotes the number of arcs in the</span></div>
<divclass="line"><aid="l00022"name="l00022"></a><spanclass="lineno"> 22</span><spanclass="comment">// Each arc (v,w) is associated a capacity c(v,w).</span></div>
<divclass="line"><aid="l00024"name="l00024"></a><spanclass="lineno"> 24</span><spanclass="comment">// A flow is a function from E to R such that:</span></div>
<divclass="line"><aid="l00026"name="l00026"></a><spanclass="lineno"> 26</span><spanclass="comment">// a) f(v,w) <= c(v,w) for all (v,w) in E (capacity constraint.)</span></div>
<divclass="line"><aid="l00028"name="l00028"></a><spanclass="lineno"> 28</span><spanclass="comment">// b) f(v,w) = -f(w,v) for all (v,w) in E (flow antisymmetry constraint.)</span></div>
<divclass="line"><aid="l00030"name="l00030"></a><spanclass="lineno"> 30</span><spanclass="comment">// c) sum on v f(v,w) = 0 (flow conservation.)</span></div>
<divclass="line"><aid="l00032"name="l00032"></a><spanclass="lineno"> 32</span><spanclass="comment">// The goal of this algorithm is to find the maximum flow from s to t, i.e.</span></div>
<divclass="line"><aid="l00033"name="l00033"></a><spanclass="lineno"> 33</span><spanclass="comment">// for example to maximize sum v f(s,v).</span></div>
<divclass="line"><aid="l00035"name="l00035"></a><spanclass="lineno"> 35</span><spanclass="comment">// The starting reference for this class of algorithms is:</span></div>
<divclass="line"><aid="l00036"name="l00036"></a><spanclass="lineno"> 36</span><spanclass="comment">// A.V. Goldberg and R.E. Tarjan. A new approach to the maximum flow problem.</span></div>
<divclass="line"><aid="l00037"name="l00037"></a><spanclass="lineno"> 37</span><spanclass="comment">// ACM Symposium on Theory of Computing, pp. 136-146.</span></div>
<divclass="line"><aid="l00040"name="l00040"></a><spanclass="lineno"> 40</span><spanclass="comment">// The basic idea of the algorithm is to handle preflows instead of flows,</span></div>
<divclass="line"><aid="l00041"name="l00041"></a><spanclass="lineno"> 41</span><spanclass="comment">// and to refine preflows until a maximum flow is obtained.</span></div>
<divclass="line"><aid="l00042"name="l00042"></a><spanclass="lineno"> 42</span><spanclass="comment">// A preflow is like a flow, except that the inflow can be larger than the</span></div>
<divclass="line"><aid="l00043"name="l00043"></a><spanclass="lineno"> 43</span><spanclass="comment">// outflow. If it is the case at a given node v, it is said that there is an</span></div>
<divclass="line"><aid="l00044"name="l00044"></a><spanclass="lineno"> 44</span><spanclass="comment">// excess at node v, and inflow = outflow + excess.</span></div>
<divclass="line"><aid="l00046"name="l00046"></a><spanclass="lineno"> 46</span><spanclass="comment">// More formally, a preflow is a function f such that:</span></div>
<divclass="line"><aid="l00048"name="l00048"></a><spanclass="lineno"> 48</span><spanclass="comment">// 1) f(v,w) <= c(v,w) for all (v,w) in E (capacity constraint). c(v,w) is a</span></div>
<divclass="line"><aid="l00049"name="l00049"></a><spanclass="lineno"> 49</span><spanclass="comment">// value representing the maximum capacity for arc (v,w).</span></div>
<divclass="line"><aid="l00051"name="l00051"></a><spanclass="lineno"> 51</span><spanclass="comment">// 2) f(v,w) = -f(w,v) for all (v,w) in E (flow antisymmetry constraint)</span></div>
<divclass="line"><aid="l00053"name="l00053"></a><spanclass="lineno"> 53</span><spanclass="comment">// 3) excess(v) = sum on u f(u,v) >= 0 is the excess at node v, the</span></div>
<divclass="line"><aid="l00054"name="l00054"></a><spanclass="lineno"> 54</span><spanclass="comment">// algebraic sum of all the incoming preflows at this node.</span></div>
<divclass="line"><aid="l00056"name="l00056"></a><spanclass="lineno"> 56</span><spanclass="comment">// Each node has an associated "height", in addition to its excess. The</span></div>
<divclass="line"><aid="l00057"name="l00057"></a><spanclass="lineno"> 57</span><spanclass="comment">// height of the source is defined to be equal to n, and cannot change. The</span></div>
<divclass="line"><aid="l00058"name="l00058"></a><spanclass="lineno"> 58</span><spanclass="comment">// height of the target is defined to be zero, and cannot change either. The</span></div>
<divclass="line"><aid="l00059"name="l00059"></a><spanclass="lineno"> 59</span><spanclass="comment">// height of all the other nodes is initialized at zero and is updated during</span></div>
<divclass="line"><aid="l00060"name="l00060"></a><spanclass="lineno"> 60</span><spanclass="comment">// the algorithm (see below). For those who want to know the details, the height</span></div>
<divclass="line"><aid="l00061"name="l00061"></a><spanclass="lineno"> 61</span><spanclass="comment">// of a node, corresponds to a reduced cost, and this enables one to prove that</span></div>
<divclass="line"><aid="l00062"name="l00062"></a><spanclass="lineno"> 62</span><spanclass="comment">// the algorithm actually computes the max flow. Note that the height of a node</span></div>
<divclass="line"><aid="l00063"name="l00063"></a><spanclass="lineno"> 63</span><spanclass="comment">// can be initialized to the distance to the target node in terms of number of</span></div>
<divclass="line"><aid="l00064"name="l00064"></a><spanclass="lineno"> 64</span><spanclass="comment">// nodes. This has not been tried in this implementation.</span></div>
<divclass="line"><aid="l00066"name="l00066"></a><spanclass="lineno"> 66</span><spanclass="comment">// A node v is said to be *active* if excess(v) > 0.</span></div>
<divclass="line"><aid="l00068"name="l00068"></a><spanclass="lineno"> 68</span><spanclass="comment">// In this case the following operations can be applied to it:</span></div>
<divclass="line"><aid="l00070"name="l00070"></a><spanclass="lineno"> 70</span><spanclass="comment">// - if there are *admissible* incident arcs, i.e. arcs which are not saturated,</span></div>
<divclass="line"><aid="l00071"name="l00071"></a><spanclass="lineno"> 71</span><spanclass="comment">// and whose head's height is lower than the height of the active node</span></div>
<divclass="line"><aid="l00072"name="l00072"></a><spanclass="lineno"> 72</span><spanclass="comment">// considered, a PushFlow operation can be applied. It consists in sending as</span></div>
<divclass="line"><aid="l00073"name="l00073"></a><spanclass="lineno"> 73</span><spanclass="comment">// much flow as both the excess at the node and the capacity of the arc</span></div>
<divclass="line"><aid="l00075"name="l00075"></a><spanclass="lineno"> 75</span><spanclass="comment">// - if there are no admissible arcs, the active node considered is relabeled,</span></div>
<divclass="line"><aid="l00076"name="l00076"></a><spanclass="lineno"> 76</span><spanclass="comment">// i.e. its height is increased to 1 + the minimum height of its neighboring</span></div>
<divclass="line"><aid="l00077"name="l00077"></a><spanclass="lineno"> 77</span><spanclass="comment">// nodes on admissible arcs.</span></div>
<divclass="line"><aid="l00078"name="l00078"></a><spanclass="lineno"> 78</span><spanclass="comment">// This is implemented in Discharge, which itself calls PushFlow and Relabel.</span></div>
<divclass="line"><aid="l00080"name="l00080"></a><spanclass="lineno"> 80</span><spanclass="comment">// Before running Discharge, it is necessary to initialize the algorithm with a</span></div>
<divclass="line"><aid="l00081"name="l00081"></a><spanclass="lineno"> 81</span><spanclass="comment">// preflow. This is done in InitializePreflow, which saturates all the arcs</span></div>
<divclass="line"><aid="l00082"name="l00082"></a><spanclass="lineno"> 82</span><spanclass="comment">// leaving the source node, and sets the excess at the heads of those arcs</span></div>
<divclass="line"><aid="l00085"name="l00085"></a><spanclass="lineno"> 85</span><spanclass="comment">// The algorithm terminates when there are no remaining active nodes, i.e. all</span></div>
<divclass="line"><aid="l00086"name="l00086"></a><spanclass="lineno"> 86</span><spanclass="comment">// the excesses at all nodes are equal to zero. In this case, a maximum flow is</span></div>
<divclass="line"><aid="l00089"name="l00089"></a><spanclass="lineno"> 89</span><spanclass="comment">// The complexity of this algorithm depends amongst other things on the choice</span></div>
<divclass="line"><aid="l00090"name="l00090"></a><spanclass="lineno"> 90</span><spanclass="comment">// of the next active node. It has been shown, for example in:</span></div>
<divclass="line"><aid="l00091"name="l00091"></a><spanclass="lineno"> 91</span><spanclass="comment">// L. Tuncel, "On the Complexity of Preflow-Push Algorithms for Maximum-Flow</span></div>
<divclass="line"><aid="l00094"name="l00094"></a><spanclass="lineno"> 94</span><spanclass="comment">// J. Cheriyan and K. Mehlhorn, "An analysis of the highest-level selection rule</span></div>
<divclass="line"><aid="l00095"name="l00095"></a><spanclass="lineno"> 95</span><spanclass="comment">// in the preflow-push max-flow algorithm", Information processing letters,</span></div>
<divclass="line"><aid="l00099"name="l00099"></a><spanclass="lineno"> 99</span><spanclass="comment">// ...that choosing the active node with the highest level yields a</span></div>
<divclass="line"><aid="l00100"name="l00100"></a><spanclass="lineno"> 100</span><spanclass="comment">// complexity of O(n^2 * sqrt(m)).</span></div>
<divclass="line"><aid="l00102"name="l00102"></a><spanclass="lineno"> 102</span><spanclass="comment">// TODO(user): implement the above active node choice rule.</span></div>
<divclass="line"><aid="l00104"name="l00104"></a><spanclass="lineno"> 104</span><spanclass="comment">// This has been validated experimentally in:</span></div>
<divclass="line"><aid="l00105"name="l00105"></a><spanclass="lineno"> 105</span><spanclass="comment">// R.K. Ahuja, M. Kodialam, A.K. Mishra, and J.B. Orlin, "Computational</span></div>
<divclass="line"><aid="l00106"name="l00106"></a><spanclass="lineno"> 106</span><spanclass="comment">// Investigations of Maximum Flow Algorithms", EJOR 97:509-542(1997).</span></div>
<divclass="line"><aid="l00110"name="l00110"></a><spanclass="lineno"> 110</span><spanclass="comment">// TODO(user): an alternative would be to evaluate:</span></div>
<divclass="line"><aid="l00111"name="l00111"></a><spanclass="lineno"> 111</span><spanclass="comment">// A.V. Goldberg, "The Partial Augment-Relabel Algorithm for the Maximum Flow</span></div>
<divclass="line"><aid="l00112"name="l00112"></a><spanclass="lineno"> 112</span><spanclass="comment">// Problem.” In Proceedings of Algorithms ESA, LNCS 5193:466-477, Springer 2008.</span></div>
<divclass="line"><aid="l00115"name="l00115"></a><spanclass="lineno"> 115</span><spanclass="comment">// An interesting general reference on network flows is:</span></div>
<divclass="line"><aid="l00116"name="l00116"></a><spanclass="lineno"> 116</span><spanclass="comment">// R. K. Ahuja, T. L. Magnanti, J. B. Orlin, "Network Flows: Theory, Algorithms,</span></div>
<divclass="line"><aid="l00117"name="l00117"></a><spanclass="lineno"> 117</span><spanclass="comment">// and Applications," Prentice Hall, 1993, ISBN: 978-0136175490,</span></div>
<divclass="line"><aid="l00146"name="l00146"></a><spanclass="lineno"> 146</span><spanclass="comment">// A simple and efficient max-cost flow interface. This is as fast as</span></div>
<divclass="line"><aid="l00147"name="l00147"></a><spanclass="lineno"> 147</span><spanclass="comment">// GenericMaxFlow<ReverseArcStaticGraph>, which is the fastest, but uses</span></div>
<divclass="line"><aid="l00148"name="l00148"></a><spanclass="lineno"> 148</span><spanclass="comment">// more memory in order to hide the somewhat involved construction of the</span></div>
<divclass="line"><aid="l00151"name="l00151"></a><spanclass="lineno"> 151</span><spanclass="comment">// TODO(user): If the need arises, extend this interface to support warm start.</span></div>
<divclass="line"><aid="l00154"name="l00154"></a><spanclass="lineno"> 154</span><spanclass="comment">// The constructor takes no size.</span></div>
<divclass="line"><aid="l00155"name="l00155"></a><spanclass="lineno"> 155</span><spanclass="comment">// New node indices will be created lazily by AddArcWithCapacity().</span></div>
<divclass="line"><aid="l00158"name="l00158"></a><spanclass="lineno"> 158</span><spanclass="comment">// Adds a directed arc with the given capacity from tail to head.</span></div>
<divclass="line"><aid="l00159"name="l00159"></a><spanclass="lineno"> 159</span><spanclass="comment">// * Node indices and capacity must be non-negative (>= 0).</span></div>
<divclass="line"><aid="l00160"name="l00160"></a><spanclass="lineno"> 160</span><spanclass="comment">// * Self-looping and duplicate arcs are supported.</span></div>
<divclass="line"><aid="l00161"name="l00161"></a><spanclass="lineno"> 161</span><spanclass="comment">// * After the method finishes, NumArcs() == the returned ArcIndex + 1.</span></div>
<divclass="line"><aid="l00165"name="l00165"></a><spanclass="lineno"> 165</span><spanclass="comment">// Returns the current number of nodes. This is one more than the largest</span></div>
<divclass="line"><aid="l00166"name="l00166"></a><spanclass="lineno"> 166</span><spanclass="comment">// node index seen so far in AddArcWithCapacity().</span></div>
<divclass="line"><aid="l00169"name="l00169"></a><spanclass="lineno"> 169</span><spanclass="comment">// Returns the current number of arcs in the graph.</span></div>
<divclass="line"><aid="l00173"name="l00173"></a><spanclass="lineno"> 173</span><spanclass="comment">// The implementation will crash if "arc" is not in [0, NumArcs()).</span></div>
<divclass="line"><aid="l00178"name="l00178"></a><spanclass="lineno"> 178</span><spanclass="comment">// Solves the problem (finds the maximum flow from the given source to the</span></div>
<divclass="line"><aid="l00179"name="l00179"></a><spanclass="lineno"> 179</span><spanclass="comment">// given sink), and returns the problem status.</span></div>
<divclass="line"><aid="l00181"name="l00181"></a><spanclass="lineno"> 181</span><spanclass="comment">// Solve() was called and found an optimal solution. Note that OptimalFlow()</span></div>
<divclass="line"><aid="l00182"name="l00182"></a><spanclass="lineno"> 182</span><spanclass="comment">// may be 0 which means that the sink is not reachable from the source.</span></div>
<divclass="line"><aid="l00184"name="l00184"></a><spanclass="lineno"> 184</span><spanclass="comment">// There is a flow > std::numeric_limits<FlowQuantity>::max(). Note that in</span></div>
<divclass="line"><aid="l00185"name="l00185"></a><spanclass="lineno"> 185</span><spanclass="comment">// this case, the class will contain a solution with a flow reaching that</span></div>
<divclass="line"><aid="l00188"name="l00188"></a><spanclass="lineno"> 188</span><spanclass="comment">// TODO(user): rename POSSIBLE_OVERFLOW to INT_OVERFLOW and modify our</span></div>
<divclass="line"><aid="l00191"name="l00191"></a><spanclass="lineno"> 191</span><spanclass="comment">// The input is inconsistent (bad tail/head/capacity values).</span></div>
<divclass="line"><aid="l00193"name="l00193"></a><spanclass="lineno"> 193</span><spanclass="comment">// This should not happen. There was an error in our code (i.e. file a bug).</span></div>
<divclass="line"><aid="l00198"name="l00198"></a><spanclass="lineno"> 198</span><spanclass="comment">// Returns the maximum flow we can send from the source to the sink in the</span></div>
<divclass="line"><aid="l00199"name="l00199"></a><spanclass="lineno"> 199</span><spanclass="comment">// last OPTIMAL Solve() context.</span></div>
<divclass="line"><aid="l00202"name="l00202"></a><spanclass="lineno"> 202</span><spanclass="comment">// Returns the flow on the given arc in the last OPTIMAL Solve() context.</span></div>
<divclass="line"><aid="l00204"name="l00204"></a><spanclass="lineno"> 204</span><spanclass="comment">// Note: It is possible that there is more than one optimal solution. The</span></div>
<divclass="line"><aid="l00205"name="l00205"></a><spanclass="lineno"> 205</span><spanclass="comment">// algorithm is deterministic so it will always return the same solution for</span></div>
<divclass="line"><aid="l00206"name="l00206"></a><spanclass="lineno"> 206</span><spanclass="comment">// a given problem. However, there is no guarantee of this from one code</span></div>
<divclass="line"><aid="l00207"name="l00207"></a><spanclass="lineno"> 207</span><spanclass="comment">// version to the next (but the code does not change often).</span></div>
<divclass="line"><aid="l00210"name="l00210"></a><spanclass="lineno"> 210</span><spanclass="comment">// Returns the nodes reachable from the source by non-saturated arcs (.i.e.</span></div>
<divclass="line"><aid="l00211"name="l00211"></a><spanclass="lineno"> 211</span><spanclass="comment">// arc with Flow(arc) < Capacity(arc)), the outgoing arcs of this set form a</span></div>
<divclass="line"><aid="l00212"name="l00212"></a><spanclass="lineno"> 212</span><spanclass="comment">// minimum cut. This works only if Solve() returned OPTIMAL.</span></div>
<divclass="line"><aid="l00215"name="l00215"></a><spanclass="lineno"> 215</span><spanclass="comment">// Returns the nodes that can reach the sink by non-saturated arcs, the</span></div>
<divclass="line"><aid="l00216"name="l00216"></a><spanclass="lineno"> 216</span><spanclass="comment">// outgoing arcs of this set form a minimum cut. Note that if this is the</span></div>
<divclass="line"><aid="l00217"name="l00217"></a><spanclass="lineno"> 217</span><spanclass="comment">// complement set of GetNodeReachableFromSource(), then the min-cut is unique.</span></div>
<divclass="line"><aid="l00218"name="l00218"></a><spanclass="lineno"> 218</span><spanclass="comment">// This works only if Solve() returned OPTIMAL.</span></div>
<divclass="line"><aid="l00223"name="l00223"></a><spanclass="lineno"> 223</span><spanclass="comment">// WARNING: This looks like it enables incremental solves, but as of 2018-02,</span></div>
<divclass="line"><aid="l00224"name="l00224"></a><spanclass="lineno"> 224</span><spanclass="comment">// the next Solve() will restart from scratch anyway.</span></div>
<divclass="line"><aid="l00225"name="l00225"></a><spanclass="lineno"> 225</span><spanclass="comment">// TODO(user): Support incrementality in the max flow implementation.</span></div>
<divclass="line"><aid="l00228"name="l00228"></a><spanclass="lineno"> 228</span><spanclass="comment">// Creates the protocol buffer representation of the current problem.</span></div>
<divclass="line"><aid="l00240"name="l00240"></a><spanclass="lineno"> 240</span><spanclass="comment">// Note that we cannot free the graph before we stop using the max-flow</span></div>
<divclass="line"><aid="l00241"name="l00241"></a><spanclass="lineno"> 241</span><spanclass="comment">// instance that uses it.</span></div>
<divclass="line"><aid="l00249"name="l00249"></a><spanclass="lineno"> 249</span><spanclass="comment">// Specific but efficient priority queue implementation. The priority type must</span></div>
<divclass="line"><aid="l00250"name="l00250"></a><spanclass="lineno"> 250</span><spanclass="comment">// be an integer. The queue allows to retrieve the element with highest priority</span></div>
<divclass="line"><aid="l00251"name="l00251"></a><spanclass="lineno"> 251</span><spanclass="comment">// but only allows pushes with a priority greater or equal to the highest</span></div>
<divclass="line"><aid="l00252"name="l00252"></a><spanclass="lineno"> 252</span><spanclass="comment">// priority in the queue minus one. All operations are in O(1) and the memory is</span></div>
<divclass="line"><aid="l00253"name="l00253"></a><spanclass="lineno"> 253</span><spanclass="comment">// in O(num elements in the queue). Elements with the same priority are</span></div>
<divclass="line"><aid="l00254"name="l00254"></a><spanclass="lineno"> 254</span><spanclass="comment">// retrieved with LIFO order.</span></div>
<divclass="line"><aid="l00256"name="l00256"></a><spanclass="lineno"> 256</span><spanclass="comment">// Note(user): As far as I know, this is an original idea and is the only code</span></div>
<divclass="line"><aid="l00257"name="l00257"></a><spanclass="lineno"> 257</span><spanclass="comment">// that use this in the Maximum Flow context. Papers usually refer to an</span></div>
<divclass="line"><aid="l00258"name="l00258"></a><spanclass="lineno"> 258</span><spanclass="comment">// height-indexed array of simple linked lists of active node with the same</span></div>
<divclass="line"><aid="l00259"name="l00259"></a><spanclass="lineno"> 259</span><spanclass="comment">// height. Even worse, sometimes they use double-linked list to allow arbitrary</span></div>
<divclass="line"><aid="l00260"name="l00260"></a><spanclass="lineno"> 260</span><spanclass="comment">// height update in order to detect missing height (used for the Gap heuristic).</span></div>
<divclass="line"><aid="l00261"name="l00261"></a><spanclass="lineno"> 261</span><spanclass="comment">// But this can actually be implemented a lot more efficiently by just</span></div>
<divclass="line"><aid="l00262"name="l00262"></a><spanclass="lineno"> 262</span><spanclass="comment">// maintaining the height distribution of all the node in the graph.</span></div>
<divclass="line"><aid="l00274"name="l00274"></a><spanclass="lineno"> 274</span><spanclass="comment">// Push a new element in the queue. Its priority must be greater or equal to</span></div>
<divclass="line"><aid="l00275"name="l00275"></a><spanclass="lineno"> 275</span><spanclass="comment">// the highest priority present in the queue, minus one. This condition is</span></div>
<divclass="line"><aid="l00276"name="l00276"></a><spanclass="lineno"> 276</span><spanclass="comment">// DCHECKed, and violating it yields erroneous queue behavior in NDEBUG mode.</span></div>
<divclass="line"><aid="l00279"name="l00279"></a><spanclass="lineno"> 279</span><spanclass="comment">// Returns the element with highest priority and remove it from the queue.</span></div>
<divclass="line"><aid="l00280"name="l00280"></a><spanclass="lineno"> 280</span><spanclass="comment">// IsEmpty() must be false, this condition is DCHECKed.</span></div>
<divclass="line"><aid="l00281"name="l00281"></a><spanclass="lineno"><aclass="line"href="classoperations__research_1_1_priority_queue_with_restricted_push.html#af355586ee86be2298efe2e81367eeffe"> 281</a></span> Element <aclass="code hl_function"href="classoperations__research_1_1_priority_queue_with_restricted_push.html#af355586ee86be2298efe2e81367eeffe">Pop</a>();</div>
<divclass="line"><aid="l00284"name="l00284"></a><spanclass="lineno"> 284</span><spanclass="comment">// Helper function to get the last element of a vector and pop it.</span></div>
<divclass="line"><aid="l00285"name="l00285"></a><spanclass="lineno"> 285</span> Element PopBack(std::vector<std::pair<Element, IntegerPriority>>* queue);</div>
<divclass="line"><aid="l00287"name="l00287"></a><spanclass="lineno"> 287</span><spanclass="comment">// This is the heart of the algorithm. basically we split the elements by</span></div>
<divclass="line"><aid="l00288"name="l00288"></a><spanclass="lineno"> 288</span><spanclass="comment">// parity of their priority and the precondition on the Push() ensures that</span></div>
<divclass="line"><aid="l00289"name="l00289"></a><spanclass="lineno"> 289</span><spanclass="comment">// both vectors are always sorted by increasing priority.</span></div>
<divclass="line"><aid="l00296"name="l00296"></a><spanclass="lineno"> 296</span><spanclass="comment">// We want an enum for the Status of a max flow run, and we want this</span></div>
<divclass="line"><aid="l00297"name="l00297"></a><spanclass="lineno"> 297</span><spanclass="comment">// enum to be scoped under GenericMaxFlow<>. Unfortunately, swig</span></div>
<divclass="line"><aid="l00298"name="l00298"></a><spanclass="lineno"> 298</span><spanclass="comment">// doesn't handle templated enums very well, so we need a base,</span></div>
<divclass="line"><aid="l00299"name="l00299"></a><spanclass="lineno"> 299</span><spanclass="comment">// untemplated class to hold it.</span></div>
<divclass="line"><aid="l00303"name="l00303"></a><spanclass="lineno"><aclass="line"href="classoperations__research_1_1_max_flow_status_class.html#a67a0db04d321a74b7e7fcfd3f1a3f70ba0e6873a155f86a4695f463bf8601d05f"> 303</a></span><aclass="code hl_enumvalue"href="classoperations__research_1_1_max_flow_status_class.html#a67a0db04d321a74b7e7fcfd3f1a3f70ba0e6873a155f86a4695f463bf8601d05f">NOT_SOLVED</a>, <spanclass="comment">// The problem was not solved, or its data were edited.</span></div>
<divclass="line"><aid="l00304"name="l00304"></a><spanclass="lineno"><aclass="line"href="classoperations__research_1_1_max_flow_status_class.html#a67a0db04d321a74b7e7fcfd3f1a3f70ba2579881e7c83261bc21bafb5a5c92cad"> 304</a></span><aclass="code hl_enumvalue"href="classoperations__research_1_1_max_flow_status_class.html#a67a0db04d321a74b7e7fcfd3f1a3f70ba2579881e7c83261bc21bafb5a5c92cad">OPTIMAL</a>, <spanclass="comment">// Solve() was called and found an optimal solution.</span></div>
<divclass="line"><aid="l00305"name="l00305"></a><spanclass="lineno"><aclass="line"href="classoperations__research_1_1_max_flow_status_class.html#a67a0db04d321a74b7e7fcfd3f1a3f70baef3d63bf3419a0e33cd86b58fadc640b"> 305</a></span><aclass="code hl_enumvalue"href="classoperations__research_1_1_max_flow_status_class.html#a67a0db04d321a74b7e7fcfd3f1a3f70baef3d63bf3419a0e33cd86b58fadc640b">INT_OVERFLOW</a>, <spanclass="comment">// There is a feasible flow > max possible flow.</span></div>
<divclass="line"><aid="l00306"name="l00306"></a><spanclass="lineno"><aclass="line"href="classoperations__research_1_1_max_flow_status_class.html#a67a0db04d321a74b7e7fcfd3f1a3f70bac2811be86dd03c6735e3b0db51ad914f"> 306</a></span><aclass="code hl_enumvalue"href="classoperations__research_1_1_max_flow_status_class.html#a67a0db04d321a74b7e7fcfd3f1a3f70bac2811be86dd03c6735e3b0db51ad914f">BAD_INPUT</a>, <spanclass="comment">// The input is inconsistent.</span></div>
<divclass="line"><aid="l00307"name="l00307"></a><spanclass="lineno"> 307</span><aclass="code hl_enumvalue"href="classoperations__research_1_1_max_flow_status_class.html#a67a0db04d321a74b7e7fcfd3f1a3f70bac76ecfd837bdc6d4150bb02c403356e4">BAD_RESULT</a><spanclass="comment">// There was an error.</span></div>
<divclass="line"><aid="l00311"name="l00311"></a><spanclass="lineno"> 311</span><spanclass="comment">// Generic MaxFlow (there is a default MaxFlow specialization defined below)</span></div>
<divclass="line"><aid="l00312"name="l00312"></a><spanclass="lineno"> 312</span><spanclass="comment">// that works with StarGraph and all the reverse arc graphs from graph.h, see</span></div>
<divclass="line"><aid="l00313"name="l00313"></a><spanclass="lineno"> 313</span><spanclass="comment">// the end of max_flow.cc for the exact types this class is compiled for.</span></div>
<divclass="line"><aid="l00325"name="l00325"></a><spanclass="lineno"> 325</span><spanclass="comment">// The height of a node never excess 2 times the number of node, so we</span></div>
<divclass="line"><aid="l00326"name="l00326"></a><spanclass="lineno"> 326</span><spanclass="comment">// use the same type as a Node index.</span></div>
<divclass="line"><aid="l00330"name="l00330"></a><spanclass="lineno"> 330</span><spanclass="comment">// Initialize a MaxFlow instance on the given graph. The graph does not need</span></div>
<divclass="line"><aid="l00331"name="l00331"></a><spanclass="lineno"> 331</span><spanclass="comment">// to be fully built yet, but its capacity reservation are used to initialize</span></div>
<divclass="line"><aid="l00332"name="l00332"></a><spanclass="lineno"> 332</span><spanclass="comment">// the memory of this class. source and sink must also be valid node of</span></div>
<divclass="line"><aid="l00337"name="l00337"></a><spanclass="lineno"> 337</span><spanclass="comment">// Returns the graph associated to the current object.</span></div>
<divclass="line"><aid="l00340"name="l00340"></a><spanclass="lineno"> 340</span><spanclass="comment">// Returns the status of last call to Solve(). NOT_SOLVED is returned if</span></div>
<divclass="line"><aid="l00341"name="l00341"></a><spanclass="lineno"> 341</span><spanclass="comment">// Solve() has never been called or if the problem has been modified in such a</span></div>
<divclass="line"><aid="l00342"name="l00342"></a><spanclass="lineno"> 342</span><spanclass="comment">// way that the previous solution becomes invalid.</span></div>
<divclass="line"><aid="l00345"name="l00345"></a><spanclass="lineno"> 345</span><spanclass="comment">// Returns the index of the node corresponding to the source of the network.</span></div>
<divclass="line"><aid="l00348"name="l00348"></a><spanclass="lineno"> 348</span><spanclass="comment">// Returns the index of the node corresponding to the sink of the network.</span></div>
<divclass="line"><aid="l00351"name="l00351"></a><spanclass="lineno"> 351</span><spanclass="comment">// Sets the capacity for arc to new_capacity.</span></div>
<divclass="line"><aid="l00357"name="l00357"></a><spanclass="lineno"> 357</span><spanclass="comment">// Returns true if a maximum flow was solved.</span></div>
<divclass="line"><aid="l00360"name="l00360"></a><spanclass="lineno"> 360</span><spanclass="comment">// Returns the total flow found by the algorithm.</span></div>
<divclass="line"><aid="l00363"name="l00363"></a><spanclass="lineno"> 363</span><spanclass="comment">// Returns the flow on arc using the equations given in the comment on</span></div>
<divclass="line"><aid="l00373"name="l00373"></a><spanclass="lineno"> 373</span><spanclass="comment">// Returns the capacity of arc using the equations given in the comment on</span></div>
<divclass="line"><aid="l00384"name="l00384"></a><spanclass="lineno"> 384</span><spanclass="comment">// Returns the nodes reachable from the source in the residual graph, the</span></div>
<divclass="line"><aid="l00385"name="l00385"></a><spanclass="lineno"> 385</span><spanclass="comment">// outgoing arcs of this set form a minimum cut.</span></div>
<divclass="line"><aid="l00388"name="l00388"></a><spanclass="lineno"> 388</span><spanclass="comment">// Returns the nodes that can reach the sink in the residual graph, the</span></div>
<divclass="line"><aid="l00389"name="l00389"></a><spanclass="lineno"> 389</span><spanclass="comment">// outgoing arcs of this set form a minimum cut. Note that if this is the</span></div>
<divclass="line"><aid="l00390"name="l00390"></a><spanclass="lineno"> 390</span><spanclass="comment">// complement of GetNodeReachableFromSource(), then the min-cut is unique.</span></div>
<divclass="line"><aid="l00392"name="l00392"></a><spanclass="lineno"> 392</span><spanclass="comment">// TODO(user): In the two-phases algorithm, we can get this minimum cut</span></div>
<divclass="line"><aid="l00393"name="l00393"></a><spanclass="lineno"> 393</span><spanclass="comment">// without doing the second phase. Add an option for this if there is a need</span></div>
<divclass="line"><aid="l00394"name="l00394"></a><spanclass="lineno"> 394</span><spanclass="comment">// to, note that the second phase is pretty fast so the gain will be small.</span></div>
<divclass="line"><aid="l00397"name="l00397"></a><spanclass="lineno"> 397</span><spanclass="comment">// Checks the consistency of the input, i.e. that capacities on the arcs are</span></div>
<divclass="line"><aid="l00398"name="l00398"></a><spanclass="lineno"> 398</span><spanclass="comment">// non-negative or null.</span></div>
<divclass="line"><aid="l00401"name="l00401"></a><spanclass="lineno"> 401</span><spanclass="comment">// Checks whether the result is valid, i.e. that node excesses are all equal</span></div>
<divclass="line"><aid="l00402"name="l00402"></a><spanclass="lineno"> 402</span><spanclass="comment">// to zero (we have a flow) and that residual capacities are all non-negative</span></div>
<divclass="line"><aid="l00403"name="l00403"></a><spanclass="lineno"> 403</span><spanclass="comment">// or zero.</span></div>
<divclass="line"><aid="l00406"name="l00406"></a><spanclass="lineno"> 406</span><spanclass="comment">// Returns true if there exists a path from the source to the sink with</span></div>
<divclass="line"><aid="l00407"name="l00407"></a><spanclass="lineno"> 407</span><spanclass="comment">// remaining capacity. This allows us to easily check at the end that the flow</span></div>
<divclass="line"><aid="l00408"name="l00408"></a><spanclass="lineno"> 408</span><spanclass="comment">// we computed is indeed optimal (provided that all the conditions tested by</span></div>
<divclass="line"><aid="l00409"name="l00409"></a><spanclass="lineno"> 409</span><spanclass="comment">// CheckResult() also hold).</span></div>
<divclass="line"><aid="l00412"name="l00412"></a><spanclass="lineno"> 412</span><spanclass="comment">// Sets the different algorithm options. All default to true.</span></div>
<divclass="line"><aid="l00413"name="l00413"></a><spanclass="lineno"> 413</span><spanclass="comment">// See the corresponding variable declaration below for more details.</span></div>
<divclass="line"><aid="l00425"name="l00425"></a><spanclass="lineno"> 425</span><spanclass="comment">// Returns the protocol buffer representation of the current problem.</span></div>
<divclass="line"><aid="l00435"name="l00435"></a><spanclass="lineno"> 435</span><spanclass="comment">// Returns true if node is active, i.e. if its excess is positive and it</span></div>
<divclass="line"><aid="l00436"name="l00436"></a><spanclass="lineno"> 436</span><spanclass="comment">// is neither the source or the sink of the graph.</span></div>
<divclass="line"><aid="l00441"name="l00441"></a><spanclass="lineno"> 441</span><spanclass="comment">// Sets the capacity of arc to 'capacity' and clears the flow on arc.</span></div>
<divclass="line"><aid="l00447"name="l00447"></a><spanclass="lineno"> 447</span><spanclass="comment">// Returns true if a precondition for Relabel is met, i.e. the outgoing arcs</span></div>
<divclass="line"><aid="l00448"name="l00448"></a><spanclass="lineno"> 448</span><spanclass="comment">// of node are all either saturated or the heights of their heads are greater</span></div>
<divclass="line"><aid="l00449"name="l00449"></a><spanclass="lineno"> 449</span><spanclass="comment">// or equal to the height of node.</span></div>
<divclass="line"><aid="l00452"name="l00452"></a><spanclass="lineno"> 452</span><spanclass="comment">// Returns context concatenated with information about arc</span></div>
<divclass="line"><aid="l00453"name="l00453"></a><spanclass="lineno"> 453</span><spanclass="comment">// in a human-friendly way.</span></div>
<divclass="line"><aid="l00459"name="l00459"></a><spanclass="lineno"> 459</span><spanclass="comment">// Get the first element from the active node container.</span></div>
<divclass="line"><aid="l00467"name="l00467"></a><spanclass="lineno"> 467</span><spanclass="comment">// Push element to the active node container.</span></div>
<divclass="line"><aid="l00476"name="l00476"></a><spanclass="lineno"> 476</span><spanclass="comment">// Check the emptiness of the container.</span></div>
<divclass="line"><aid="l00489"name="l00489"></a><spanclass="lineno"> 489</span><spanclass="comment">// Discharges an active node node by saturating its admissible adjacent arcs,</span></div>
<divclass="line"><aid="l00490"name="l00490"></a><spanclass="lineno"> 490</span><spanclass="comment">// if any, and by relabelling it when it becomes inactive.</span></div>
<divclass="line"><aid="l00493"name="l00493"></a><spanclass="lineno"> 493</span><spanclass="comment">// Initializes the preflow to a state that enables to run Refine.</span></div>
<divclass="line"><aid="l00496"name="l00496"></a><spanclass="lineno"> 496</span><spanclass="comment">// Clears the flow excess at each node by pushing the flow back to the source:</span></div>
<divclass="line"><aid="l00497"name="l00497"></a><spanclass="lineno"> 497</span><spanclass="comment">// - Do a depth-first search from the source in the direct graph to cancel</span></div>
<divclass="line"><aid="l00499"name="l00499"></a><spanclass="lineno"> 499</span><spanclass="comment">// - Then, return flow excess along the depth-first search tree (by pushing</span></div>
<divclass="line"><aid="l00500"name="l00500"></a><spanclass="lineno"> 500</span><spanclass="comment">// the flow in the reverse dfs topological order).</span></div>
<divclass="line"><aid="l00501"name="l00501"></a><spanclass="lineno"> 501</span><spanclass="comment">// The theoretical complexity is O(mn), but it is a lot faster in practice.</span></div>
<divclass="line"><aid="l00504"name="l00504"></a><spanclass="lineno"> 504</span><spanclass="comment">// Computes the best possible node potential given the current flow using a</span></div>
<divclass="line"><aid="l00505"name="l00505"></a><spanclass="lineno"> 505</span><spanclass="comment">// reverse breadth-first search from the sink in the reverse residual graph.</span></div>
<divclass="line"><aid="l00506"name="l00506"></a><spanclass="lineno"> 506</span><spanclass="comment">// This is an implementation of the global update heuristic mentioned in many</span></div>
<divclass="line"><aid="l00507"name="l00507"></a><spanclass="lineno"> 507</span><spanclass="comment">// max-flow papers. See for instance: B.V. Cherkassky, A.V. Goldberg, "On</span></div>
<divclass="line"><aid="l00508"name="l00508"></a><spanclass="lineno"> 508</span><spanclass="comment">// implementing push-relabel methods for the maximum flow problem",</span></div>
<divclass="line"><aid="l00513"name="l00513"></a><spanclass="lineno"> 513</span><spanclass="comment">// Tries to saturate all the outgoing arcs from the source that can reach the</span></div>
<divclass="line"><aid="l00514"name="l00514"></a><spanclass="lineno"> 514</span><spanclass="comment">// sink. Most of the time, we can do that in one go, except when more flow</span></div>
<divclass="line"><aid="l00515"name="l00515"></a><spanclass="lineno"> 515</span><spanclass="comment">// than kMaxFlowQuantity can be pushed out of the source in which case we</span></div>
<divclass="line"><aid="l00516"name="l00516"></a><spanclass="lineno"> 516</span><spanclass="comment">// have to be careful. Returns true if some flow was pushed.</span></div>
<divclass="line"><aid="l00519"name="l00519"></a><spanclass="lineno"> 519</span><spanclass="comment">// Pushes flow on arc, i.e. consumes flow on residual_arc_capacity_[arc],</span></div>
<divclass="line"><aid="l00520"name="l00520"></a><spanclass="lineno"> 520</span><spanclass="comment">// and consumes -flow on residual_arc_capacity_[Opposite(arc)]. Updates</span></div>
<divclass="line"><aid="l00521"name="l00521"></a><spanclass="lineno"> 521</span><spanclass="comment">// node_excess_ at the tail and head of arc accordingly.</span></div>
<divclass="line"><aid="l00524"name="l00524"></a><spanclass="lineno"> 524</span><spanclass="comment">// Relabels a node, i.e. increases its height by the minimum necessary amount.</span></div>
<divclass="line"><aid="l00525"name="l00525"></a><spanclass="lineno"> 525</span><spanclass="comment">// This version of Relabel is relaxed in a way such that if an admissible arc</span></div>
<divclass="line"><aid="l00526"name="l00526"></a><spanclass="lineno"> 526</span><spanclass="comment">// exists at the current node height, then the node is not relabeled. This</span></div>
<divclass="line"><aid="l00527"name="l00527"></a><spanclass="lineno"> 527</span><spanclass="comment">// enables us to deal with wrong values of first_admissible_arc_[node] when</span></div>
<divclass="line"><aid="l00528"name="l00528"></a><spanclass="lineno"> 528</span><spanclass="comment">// updating it is too costly.</span></div>
<divclass="line"><aid="l00531"name="l00531"></a><spanclass="lineno"> 531</span><spanclass="comment">// Handy member functions to make the code more compact.</span></div>
<divclass="line"><aid="l00538"name="l00538"></a><spanclass="lineno"> 538</span><spanclass="comment">// Returns the set of nodes reachable from start in the residual graph or in</span></div>
<divclass="line"><aid="l00539"name="l00539"></a><spanclass="lineno"> 539</span><spanclass="comment">// the reverse residual graph (if reverse is true).</span></div>
<divclass="line"><aid="l00546"name="l00546"></a><spanclass="lineno"> 546</span><spanclass="comment">// A pointer to the graph passed as argument.</span></div>
<divclass="line"><aid="l00549"name="l00549"></a><spanclass="lineno"> 549</span><spanclass="comment">// An array representing the excess for each node in graph_.</span></div>
<divclass="line"><aid="l00552"name="l00552"></a><spanclass="lineno"> 552</span><spanclass="comment">// An array representing the height function for each node in graph_. For a</span></div>
<divclass="line"><aid="l00553"name="l00553"></a><spanclass="lineno"> 553</span><spanclass="comment">// given node, this is a lower bound on the shortest path length from this</span></div>
<divclass="line"><aid="l00554"name="l00554"></a><spanclass="lineno"> 554</span><spanclass="comment">// node to the sink in the residual network. The height of a node always goes</span></div>
<divclass="line"><aid="l00555"name="l00555"></a><spanclass="lineno"> 555</span><spanclass="comment">// up during the course of a Solve().</span></div>
<divclass="line"><aid="l00557"name="l00557"></a><spanclass="lineno"> 557</span><spanclass="comment">// Since initially we saturate all the outgoing arcs of the source, we can</span></div>
<divclass="line"><aid="l00558"name="l00558"></a><spanclass="lineno"> 558</span><spanclass="comment">// never reach the sink from the source in the residual graph. Initially we</span></div>
<divclass="line"><aid="l00559"name="l00559"></a><spanclass="lineno"> 559</span><spanclass="comment">// set the height of the source to n (the number of node of the graph) and it</span></div>
<divclass="line"><aid="l00560"name="l00560"></a><spanclass="lineno"> 560</span><spanclass="comment">// never changes. If a node as an height >= n, then this node can't reach the</span></div>
<divclass="line"><aid="l00561"name="l00561"></a><spanclass="lineno"> 561</span><spanclass="comment">// sink and its height minus n is a lower bound on the shortest path length</span></div>
<divclass="line"><aid="l00562"name="l00562"></a><spanclass="lineno"> 562</span><spanclass="comment">// from this node to the source in the residual graph.</span></div>
<divclass="line"><aid="l00565"name="l00565"></a><spanclass="lineno"> 565</span><spanclass="comment">// An array representing the residual_capacity for each arc in graph_.</span></div>
<divclass="line"><aid="l00566"name="l00566"></a><spanclass="lineno"> 566</span><spanclass="comment">// Residual capacities enable one to represent the capacity and flow for all</span></div>
<divclass="line"><aid="l00567"name="l00567"></a><spanclass="lineno"> 567</span><spanclass="comment">// arcs in the graph in the following manner.</span></div>
<divclass="line"><aid="l00568"name="l00568"></a><spanclass="lineno"> 568</span><spanclass="comment">// For all arc, residual_arc_capacity_[arc] = capacity[arc] - flow[arc]</span></div>
<divclass="line"><aid="l00569"name="l00569"></a><spanclass="lineno"> 569</span><spanclass="comment">// Moreover, for reverse arcs, capacity[arc] = 0 by definition,</span></div>
<divclass="line"><aid="l00570"name="l00570"></a><spanclass="lineno"> 570</span><spanclass="comment">// Also flow[Opposite(arc)] = -flow[arc] by definition.</span></div>
<divclass="line"><aid="l00578"name="l00578"></a><spanclass="lineno"> 578</span><spanclass="comment">// Using these facts enables one to only maintain residual_arc_capacity_,</span></div>
<divclass="line"><aid="l00579"name="l00579"></a><spanclass="lineno"> 579</span><spanclass="comment">// instead of both capacity and flow, for each direct and indirect arc. This</span></div>
<divclass="line"><aid="l00580"name="l00580"></a><spanclass="lineno"> 580</span><spanclass="comment">// reduces the amount of memory for this information by a factor 2.</span></div>
<divclass="line"><aid="l00583"name="l00583"></a><spanclass="lineno"> 583</span><spanclass="comment">// An array representing the first admissible arc for each node in graph_.</span></div>
<divclass="line"><aid="l00586"name="l00586"></a><spanclass="lineno"> 586</span><spanclass="comment">// A stack used for managing active nodes in the algorithm.</span></div>
<divclass="line"><aid="l00587"name="l00587"></a><spanclass="lineno"> 587</span><spanclass="comment">// Note that the papers cited above recommend the use of a queue, but</span></div>
<divclass="line"><aid="l00588"name="l00588"></a><spanclass="lineno"> 588</span><spanclass="comment">// benchmarking so far has not proved it is better. In particular, processing</span></div>
<divclass="line"><aid="l00589"name="l00589"></a><spanclass="lineno"> 589</span><spanclass="comment">// nodes in LIFO order has better cache locality.</span></div>
<divclass="line"><aid="l00592"name="l00592"></a><spanclass="lineno"> 592</span><spanclass="comment">// A priority queue used for managing active nodes in the algorithm. It allows</span></div>
<divclass="line"><aid="l00593"name="l00593"></a><spanclass="lineno"> 593</span><spanclass="comment">// to select the active node with highest height before each Discharge().</span></div>
<divclass="line"><aid="l00594"name="l00594"></a><spanclass="lineno"> 594</span><spanclass="comment">// Moreover, since all pushes from this node will be to nodes with height</span></div>
<divclass="line"><aid="l00595"name="l00595"></a><spanclass="lineno"> 595</span><spanclass="comment">// greater or equal to the initial discharged node height minus one, the</span></div>
<divclass="line"><aid="l00596"name="l00596"></a><spanclass="lineno"> 596</span><spanclass="comment">// PriorityQueueWithRestrictedPush is a perfect fit.</span></div>
<divclass="line"><aid="l00599"name="l00599"></a><spanclass="lineno"> 599</span><spanclass="comment">// The index of the source node in graph_.</span></div>
<divclass="line"><aid="l00602"name="l00602"></a><spanclass="lineno"> 602</span><spanclass="comment">// The index of the sink node in graph_.</span></div>
<divclass="line"><aid="l00608"name="l00608"></a><spanclass="lineno"> 608</span><spanclass="comment">// BFS queue used by the GlobalUpdate() function. We do not use a C++ queue</span></div>
<divclass="line"><aid="l00609"name="l00609"></a><spanclass="lineno"> 609</span><spanclass="comment">// because we need access to the vector for different optimizations.</span></div>
<divclass="line"><aid="l00613"name="l00613"></a><spanclass="lineno"> 613</span><spanclass="comment">// Whether or not to use GlobalUpdate().</span></div>
<divclass="line"><aid="l00616"name="l00616"></a><spanclass="lineno"> 616</span><spanclass="comment">// Whether or not we use a two-phase algorithm:</span></div>
<divclass="line"><aid="l00617"name="l00617"></a><spanclass="lineno"> 617</span><spanclass="comment">// 1/ Only deal with nodes that can reach the sink. At the end we know the</span></div>
<divclass="line"><aid="l00618"name="l00618"></a><spanclass="lineno"> 618</span><spanclass="comment">// value of the maximum flow and we have a min-cut.</span></div>
<divclass="line"><aid="l00619"name="l00619"></a><spanclass="lineno"> 619</span><spanclass="comment">// 2/ Call PushFlowExcessBackToSource() to obtain a max-flow. This is usually</span></div>
<divclass="line"><aid="l00620"name="l00620"></a><spanclass="lineno"> 620</span><spanclass="comment">// a lot faster than the first phase.</span></div>
<divclass="line"><aid="l00623"name="l00623"></a><spanclass="lineno"> 623</span><spanclass="comment">// Whether or not we use the PriorityQueueWithRestrictedPush to process the</span></div>
<divclass="line"><aid="l00624"name="l00624"></a><spanclass="lineno"> 624</span><spanclass="comment">// active nodes rather than a simple queue. This can only be true if</span></div>
<divclass="line"><aid="l00625"name="l00625"></a><spanclass="lineno"> 625</span><spanclass="comment">// use_global_update_ is true.</span></div>
<divclass="line"><aid="l00627"name="l00627"></a><spanclass="lineno"> 627</span><spanclass="comment">// Note(user): using a template will be slightly faster, but since we test</span></div>
<divclass="line"><aid="l00628"name="l00628"></a><spanclass="lineno"> 628</span><spanclass="comment">// this in a non-critical path, this only has a minor impact.</span></div>
<divclass="line"><aid="l00631"name="l00631"></a><spanclass="lineno"> 631</span><spanclass="comment">// Whether or not we check the input, this is a small price to pay for</span></div>
<divclass="line"><aid="l00632"name="l00632"></a><spanclass="lineno"> 632</span><spanclass="comment">// robustness. Disable only if you know the input is valid because an invalid</span></div>
<divclass="line"><aid="l00633"name="l00633"></a><spanclass="lineno"> 633</span><spanclass="comment">// input can cause the algorithm to run into an infinite loop!</span></div>
<divclass="line"><aid="l00636"name="l00636"></a><spanclass="lineno"> 636</span><spanclass="comment">// Whether or not we check the result.</span></div>
<divclass="line"><aid="l00637"name="l00637"></a><spanclass="lineno"> 637</span><spanclass="comment">// TODO(user): Make the check more exhaustive by checking the optimality?</span></div>
<divclass="line"><aid="l00649"name="l00649"></a><spanclass="lineno"> 649</span><spanclass="comment">// Default instance MaxFlow that uses StarGraph. Note that we cannot just use a</span></div>
<divclass="line"><aid="l00650"name="l00650"></a><spanclass="lineno"> 650</span><spanclass="comment">// typedef because of dependent code expecting MaxFlow to be a real class.</span></div>
<divclass="line"><aid="l00651"name="l00651"></a><spanclass="lineno"> 651</span><spanclass="comment">// TODO(user): Modify this code and remove it.</span></div>
<divclass="line"><aid="l00674"name="l00674"></a><spanclass="lineno"> 674</span> Element element, IntegerPriority priority) {</div>
<divclass="line"><aid="l00675"name="l00675"></a><spanclass="lineno"> 675</span><spanclass="comment">// Since users may rely on it, we DCHECK the exact condition.</span></div>
<divclass="line"><aid="l00679"name="l00679"></a><spanclass="lineno"> 679</span><spanclass="comment">// Note that the DCHECK() below are less restrictive than the ones above but</span></div>
<divclass="line"><aid="l00680"name="l00680"></a><spanclass="lineno"> 680</span><spanclass="comment">// check a necessary and sufficient condition for the priority queue to behave</span></div>
<divclass="line"><aid="l00681"name="l00681"></a><spanclass="lineno"> 681</span><spanclass="comment">// as expected.</span></div>
<divclass="line"><aid="l00682"name="l00682"></a><spanclass="lineno"> 682</span><spanclass="keywordflow">if</span> (priority & 1) {</div>