<ahref="perfect__matching_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">// Implementation of the Blossom V min-cost perfect matching algorithm. The</span></div>
<divclass="line"><aid="l00015"name="l00015"></a><spanclass="lineno"> 15</span><spanclass="comment">// main source for the algo is the paper: "Blossom V: A new implementation</span></div>
<divclass="line"><aid="l00016"name="l00016"></a><spanclass="lineno"> 16</span><spanclass="comment">// of a minimum cost perfect matching algorithm", Vladimir Kolmogorov.</span></div>
<divclass="line"><aid="l00018"name="l00018"></a><spanclass="lineno"> 18</span><spanclass="comment">// The Algorithm is a primal-dual algorithm. It always maintains a dual-feasible</span></div>
<divclass="line"><aid="l00019"name="l00019"></a><spanclass="lineno"> 19</span><spanclass="comment">// solution. We recall some notations here, but see the paper for more details</span></div>
<divclass="line"><aid="l00020"name="l00020"></a><spanclass="lineno"> 20</span><spanclass="comment">// as it is well written.</span></div>
<divclass="line"><aid="l00022"name="l00022"></a><spanclass="lineno"> 22</span><spanclass="comment">// TODO(user): This is a work in progress. The algo is not fully implemented</span></div>
<divclass="line"><aid="l00023"name="l00023"></a><spanclass="lineno"> 23</span><spanclass="comment">// yet. The initial version is closer to Blossom IV since we update the dual</span></div>
<divclass="line"><aid="l00024"name="l00024"></a><spanclass="lineno"> 24</span><spanclass="comment">// values for all trees at once with the same delta.</span></div>
<divclass="line"><aid="l00048"name="l00048"></a><spanclass="lineno"> 48</span><spanclass="comment">// Given an undirected graph with costs on each edges, this class allows to</span></div>
<divclass="line"><aid="l00049"name="l00049"></a><spanclass="lineno"> 49</span><spanclass="comment">// compute a perfect matching with minimum cost. A matching is a set of disjoint</span></div>
<divclass="line"><aid="l00050"name="l00050"></a><spanclass="lineno"> 50</span><spanclass="comment">// pairs of nodes connected by an edge. The matching is perfect if all nodes are</span></div>
<divclass="line"><aid="l00051"name="l00051"></a><spanclass="lineno"> 51</span><spanclass="comment">// matched to each others.</span></div>
<divclass="line"><aid="l00054"name="l00054"></a><spanclass="lineno"> 54</span><spanclass="comment">// TODO(user): For now we ask the number of nodes at construction, but we</span></div>
<divclass="line"><aid="l00055"name="l00055"></a><spanclass="lineno"> 55</span><spanclass="comment">// could automatically infer it from the added edges if needed.</span></div>
<divclass="line"><aid="l00061"name="l00061"></a><spanclass="lineno"> 61</span><spanclass="comment">// TODO(user): Eventually, we may support incremental Solves(). Or at least</span></div>
<divclass="line"><aid="l00062"name="l00062"></a><spanclass="lineno"> 62</span><spanclass="comment">// memory reuse if one wants to solve many problems in a row.</span></div>
<divclass="line"><aid="l00065"name="l00065"></a><spanclass="lineno"> 65</span><spanclass="comment">// Adds an undirected edges between the two given nodes.</span></div>
<divclass="line"><aid="l00067"name="l00067"></a><spanclass="lineno"> 67</span><spanclass="comment">// For now we only accept non-negative cost.</span></div>
<divclass="line"><aid="l00068"name="l00068"></a><spanclass="lineno"> 68</span><spanclass="comment">// TODO(user): We can easily shift all costs if negative costs are needed.</span></div>
<divclass="line"><aid="l00070"name="l00070"></a><spanclass="lineno"> 70</span><spanclass="comment">// Important: The algorithm supports multi-edges, but it will be slower. So it</span></div>
<divclass="line"><aid="l00071"name="l00071"></a><spanclass="lineno"> 71</span><spanclass="comment">// is better to only add one edge with a minimum cost between two nodes. In</span></div>
<divclass="line"><aid="l00072"name="l00072"></a><spanclass="lineno"> 72</span><spanclass="comment">// particular, do not add both AddEdge(a, b, cost) and AddEdge(b, a, cost).</span></div>
<divclass="line"><aid="l00073"name="l00073"></a><spanclass="lineno"> 73</span><spanclass="comment">// TODO(user): We could just presolve them away.</span></div>
<divclass="line"><aid="l00076"name="l00076"></a><spanclass="lineno"> 76</span><spanclass="comment">// Solves the min-cost perfect matching problem on the given graph.</span></div>
<divclass="line"><aid="l00078"name="l00078"></a><spanclass="lineno"> 78</span><spanclass="comment">// NOTE(user): If needed we could support a time limit. Aborting early will</span></div>
<divclass="line"><aid="l00079"name="l00079"></a><spanclass="lineno"> 79</span><spanclass="comment">// not provide a perfect matching, but the algorithm does maintain a valid</span></div>
<divclass="line"><aid="l00080"name="l00080"></a><spanclass="lineno"> 80</span><spanclass="comment">// lower bound on the optimal cost that gets better and better during</span></div>
<divclass="line"><aid="l00081"name="l00081"></a><spanclass="lineno"> 81</span><spanclass="comment">// execution until it reaches the optimal value. Similarly, it is easy to</span></div>
<divclass="line"><aid="l00082"name="l00082"></a><spanclass="lineno"> 82</span><spanclass="comment">// support an early stop if this bound crosses a preset threshold.</span></div>
<divclass="line"><aid="l00084"name="l00084"></a><spanclass="lineno"> 84</span><spanclass="comment">// A perfect matching with min-cost has been found.</span></div>
<divclass="line"><aid="l00087"name="l00087"></a><spanclass="lineno"> 87</span><spanclass="comment">// There is no perfect matching in this graph.</span></div>
<divclass="line"><aid="l00090"name="l00090"></a><spanclass="lineno"> 90</span><spanclass="comment">// The costs are too large and caused an overflow during the algorithm</span></div>
<divclass="line"><aid="l00094"name="l00094"></a><spanclass="lineno"> 94</span><spanclass="comment">// Advanced usage: the matching is OPTIMAL and was computed without</span></div>
<divclass="line"><aid="l00095"name="l00095"></a><spanclass="lineno"> 95</span><spanclass="comment">// overflow, but its OptimalCost() does not fit on an int64_t. Note that</span></div>
<divclass="line"><aid="l00096"name="l00096"></a><spanclass="lineno"> 96</span><spanclass="comment">// Match() still work and you can re-compute the cost in double for</span></div>
<divclass="line"><aid="l00102"name="l00102"></a><spanclass="lineno"> 102</span><spanclass="comment">// Returns the cost of the perfect matching. Only valid when the last solve</span></div>
<divclass="line"><aid="l00103"name="l00103"></a><spanclass="lineno"> 103</span><spanclass="comment">// status was OPTIMAL.</span></div>
<divclass="line"><aid="l00109"name="l00109"></a><spanclass="lineno"> 109</span><spanclass="comment">// Returns the node matched to the given node. In a perfect matching all nodes</span></div>
<divclass="line"><aid="l00110"name="l00110"></a><spanclass="lineno"> 110</span><spanclass="comment">// have a match. Only valid when the last solve status was OPTIMAL.</span></div>
<divclass="line"><aid="l00123"name="l00123"></a><spanclass="lineno"> 123</span><spanclass="comment">// Fields used to report the optimal solution. Most of it could be read on</span></div>
<divclass="line"><aid="l00124"name="l00124"></a><spanclass="lineno"> 124</span><spanclass="comment">// the fly from BlossomGraph, but we prefer to copy them here. This allows to</span></div>
<divclass="line"><aid="l00125"name="l00125"></a><spanclass="lineno"> 125</span><spanclass="comment">// reclaim the memory of graph_ early or allows to still query the last</span></div>
<divclass="line"><aid="l00126"name="l00126"></a><spanclass="lineno"> 126</span><spanclass="comment">// solution if we later allow re-solve with incremental changes to the graph.</span></div>
<divclass="line"><aid="l00133"name="l00133"></a><spanclass="lineno"> 133</span><spanclass="comment">// Class containing the main data structure used by the Blossom algorithm.</span></div>
<divclass="line"><aid="l00135"name="l00135"></a><spanclass="lineno"> 135</span><spanclass="comment">// At the core is the original undirected graph. During the algorithm execution</span></div>
<divclass="line"><aid="l00136"name="l00136"></a><spanclass="lineno"> 136</span><spanclass="comment">// we might collapse nodes into so-called Blossoms. A Blossom is a cycle of</span></div>
<divclass="line"><aid="l00137"name="l00137"></a><spanclass="lineno"> 137</span><spanclass="comment">// external nodes (which can be blossom nodes) of odd length (>= 3). The edges</span></div>
<divclass="line"><aid="l00138"name="l00138"></a><spanclass="lineno"> 138</span><spanclass="comment">// of the cycle are called blossom-forming eges and will always be tight</span></div>
<divclass="line"><aid="l00139"name="l00139"></a><spanclass="lineno"> 139</span><spanclass="comment">// (i.e. have a slack of zero). Once a Blossom is created, its nodes become</span></div>
<divclass="line"><aid="l00140"name="l00140"></a><spanclass="lineno"> 140</span><spanclass="comment">// "internal" and are basically considered merged into the blossom node for the</span></div>
<divclass="line"><aid="l00141"name="l00141"></a><spanclass="lineno"> 141</span><spanclass="comment">// rest of the algorithm (except if we later re-expand the blossom).</span></div>
<divclass="line"><aid="l00143"name="l00143"></a><spanclass="lineno"> 143</span><spanclass="comment">// Moreover, external nodes of the graph will have 3 possible types ([+], [-]</span></div>
<divclass="line"><aid="l00144"name="l00144"></a><spanclass="lineno"> 144</span><spanclass="comment">// and free [0]). Free nodes will always be matched together in pairs. Nodes of</span></div>
<divclass="line"><aid="l00145"name="l00145"></a><spanclass="lineno"> 145</span><spanclass="comment">// type [+] and [-] are arranged in a forest of alternating [+]/[-] disjoint</span></div>
<divclass="line"><aid="l00146"name="l00146"></a><spanclass="lineno"> 146</span><spanclass="comment">// trees. Each unmatched node is the root of a tree, and of type [+]. Nodes [-]</span></div>
<divclass="line"><aid="l00147"name="l00147"></a><spanclass="lineno"> 147</span><spanclass="comment">// will always have exactly one child to witch they are matched. [+] nodes can</span></div>
<divclass="line"><aid="l00148"name="l00148"></a><spanclass="lineno"> 148</span><spanclass="comment">// have any number of [-] children, to which they are not matched. All the edges</span></div>
<divclass="line"><aid="l00149"name="l00149"></a><spanclass="lineno"> 149</span><spanclass="comment">// of the trees will always be tight. Some examples below, double edges are used</span></div>
<divclass="line"><aid="l00150"name="l00150"></a><spanclass="lineno"> 150</span><spanclass="comment">// for matched nodes:</span></div>
<divclass="line"><aid="l00160"name="l00160"></a><spanclass="lineno"> 160</span><spanclass="comment">// A single unmatched node is also a tree: [+]</span></div>
<divclass="line"><aid="l00162"name="l00162"></a><spanclass="lineno"> 162</span><spanclass="comment">// TODO(user): For now this class does not maintain a second graph of edges</span></div>
<divclass="line"><aid="l00163"name="l00163"></a><spanclass="lineno"> 163</span><spanclass="comment">// between the trees nor does it maintains priority queue of edges.</span></div>
<divclass="line"><aid="l00165"name="l00165"></a><spanclass="lineno"> 165</span><spanclass="comment">// TODO(user): For now we use CHECKs in many places to facilitate development.</span></div>
<divclass="line"><aid="l00166"name="l00166"></a><spanclass="lineno"> 166</span><spanclass="comment">// Switch them to DCHECKs for speed once the code is more stable.</span></div>
<divclass="line"><aid="l00175"name="l00175"></a><spanclass="lineno"> 175</span><spanclass="comment">// NOTE(user): Those can't be constexpr because of the or-tools export,</span></div>
<divclass="line"><aid="l00176"name="l00176"></a><spanclass="lineno"> 176</span><spanclass="comment">// which complains for constexpr DEFINE_STRONG_INT_TYPE.</span></div>
<divclass="line"><aid="l00181"name="l00181"></a><spanclass="lineno"> 181</span><spanclass="comment">// Node related data.</span></div>
<divclass="line"><aid="l00182"name="l00182"></a><spanclass="lineno"> 182</span><spanclass="comment">// We store the edges incident to a node separately in the graph_ member.</span></div>
<divclass="line"><aid="l00186"name="l00186"></a><spanclass="lineno"> 186</span><spanclass="comment">// A node can be in one of these 4 exclusive states. Internal nodes are part</span></div>
<divclass="line"><aid="l00187"name="l00187"></a><spanclass="lineno"> 187</span><spanclass="comment">// of a Blossom and should be ignored until this Blossom is expanded. All</span></div>
<divclass="line"><aid="l00188"name="l00188"></a><spanclass="lineno"> 188</span><spanclass="comment">// the other nodes are "external". A free node is always matched to another</span></div>
<divclass="line"><aid="l00189"name="l00189"></a><spanclass="lineno"> 189</span><spanclass="comment">// free node. All the other external node are in alternating [+]/[-] trees</span></div>
<divclass="line"><aid="l00190"name="l00190"></a><spanclass="lineno"> 190</span><spanclass="comment">// rooted at the only unmatched node of the tree (always of type [+]).</span></div>
<divclass="line"><aid="l00199"name="l00199"></a><spanclass="lineno"> 199</span><spanclass="comment">// Is this node a blossom? if yes, it was formed by merging the node.blossom</span></div>
<divclass="line"><aid="l00200"name="l00200"></a><spanclass="lineno"> 200</span><spanclass="comment">// nodes together. Note that we reuse the index of node.blossom[0] for this</span></div>
<divclass="line"><aid="l00201"name="l00201"></a><spanclass="lineno"> 201</span><spanclass="comment">// blossom node. A blossom node can be of any type.</span></div>
<divclass="line"><aid="l00204"name="l00204"></a><spanclass="lineno"> 204</span><spanclass="comment">// The type of this node. We use an int for convenience in the update</span></div>
<divclass="line"><aid="l00205"name="l00205"></a><spanclass="lineno"> 205</span><spanclass="comment">// formulas. This is 1 for [+] nodes, -1 for [-] nodes and 0 for all the</span></div>
<divclass="line"><aid="l00208"name="l00208"></a><spanclass="lineno"> 208</span><spanclass="comment">// Internal node also have a type of zero so the dual formula are correct.</span></div>
<divclass="line"><aid="l00211"name="l00211"></a><spanclass="lineno"> 211</span><spanclass="comment">// Whether this node is part of a blossom.</span></div>
<divclass="line"><aid="l00214"name="l00214"></a><spanclass="lineno"> 214</span><spanclass="comment">// The parent of this node in its tree or itself otherwise.</span></div>
<divclass="line"><aid="l00215"name="l00215"></a><spanclass="lineno"> 215</span><spanclass="comment">// Unused for internal nodes.</span></div>
<divclass="line"><aid="l00218"name="l00218"></a><spanclass="lineno"> 218</span><spanclass="comment">// Itself if not matched, or this node match otherwise.</span></div>
<divclass="line"><aid="l00219"name="l00219"></a><spanclass="lineno"> 219</span><spanclass="comment">// Unused for internal nodes.</span></div>
<divclass="line"><aid="l00222"name="l00222"></a><spanclass="lineno"> 222</span><spanclass="comment">// The root of this tree which never changes until a tree is disassambled by</span></div>
<divclass="line"><aid="l00223"name="l00223"></a><spanclass="lineno"> 223</span><spanclass="comment">// an Augment(). Unused for internal nodes.</span></div>
<divclass="line"><aid="l00226"name="l00226"></a><spanclass="lineno"> 226</span><spanclass="comment">// The "delta" to apply to get the dual for nodes of this tree.</span></div>
<divclass="line"><aid="l00227"name="l00227"></a><spanclass="lineno"> 227</span><spanclass="comment">// This is only filled for root nodes (i.e unmatched nodes).</span></div>
<divclass="line"><aid="l00230"name="l00230"></a><spanclass="lineno"> 230</span><spanclass="comment">// See the formula in Dual() used to derive the true dual of this node.</span></div>
<divclass="line"><aid="l00231"name="l00231"></a><spanclass="lineno"> 231</span><spanclass="comment">// This is the equal to the "true" dual for free exterior node and internal</span></div>
<divclass="line"><aid="l00236"name="l00236"></a><spanclass="lineno"> 236</span><spanclass="comment">// The true dual of this node. We only maintain this in debug mode.</span></div>
<divclass="line"><aid="l00240"name="l00240"></a><spanclass="lineno"> 240</span><spanclass="comment">// Non-empty for Blossom only. The odd-cycle of blossom nodes that form this</span></div>
<divclass="line"><aid="l00241"name="l00241"></a><spanclass="lineno"> 241</span><spanclass="comment">// blossom. The first element should always be the current blossom node, and</span></div>
<divclass="line"><aid="l00242"name="l00242"></a><spanclass="lineno"> 242</span><spanclass="comment">// all the other nodes are internal nodes.</span></div>
<divclass="line"><aid="l00245"name="l00245"></a><spanclass="lineno"> 245</span><spanclass="comment">// This allows to store information about a new blossom node created by</span></div>
<divclass="line"><aid="l00246"name="l00246"></a><spanclass="lineno"> 246</span><spanclass="comment">// Shrink() so that we can properly restore it on Expand(). Note that we</span></div>
<divclass="line"><aid="l00247"name="l00247"></a><spanclass="lineno"> 247</span><spanclass="comment">// store the saved information on the second node of a blossom cycle (and</span></div>
<divclass="line"><aid="l00248"name="l00248"></a><spanclass="lineno"> 248</span><spanclass="comment">// not the blossom node itself) because that node will be "hidden" until the</span></div>
<divclass="line"><aid="l00249"name="l00249"></a><spanclass="lineno"> 249</span><spanclass="comment">// blossom is expanded so this way, we do not need more than one set of</span></div>
<divclass="line"><aid="l00250"name="l00250"></a><spanclass="lineno"> 250</span><spanclass="comment">// saved information per node.</span></div>
<divclass="line"><aid="l00258"name="l00258"></a><spanclass="lineno"> 258</span><spanclass="comment">// An undirected edge between two nodes: tail <-> head.</span></div>
<divclass="line"><aid="l00269"name="l00269"></a><spanclass="lineno"> 269</span><spanclass="comment">// Returns the "other" end of this edge.</span></div>
<divclass="line"><aid="l00275"name="l00275"></a><spanclass="lineno"> 275</span><spanclass="comment">// AdjustablePriorityQueue interface. Note that we use std::greater<> in</span></div>
<divclass="line"><aid="l00276"name="l00276"></a><spanclass="lineno"> 276</span><spanclass="comment">// our queues since we want the lowest pseudo_slack first.</span></div>
<divclass="line"><aid="l00283"name="l00283"></a><spanclass="lineno"> 283</span><spanclass="comment">// See the formula is Slack() used to derive the true slack of this edge.</span></div>
<divclass="line"><aid="l00287"name="l00287"></a><spanclass="lineno"> 287</span><spanclass="comment">// We only maintain this in debug mode.</span></div>
<divclass="line"><aid="l00291"name="l00291"></a><spanclass="lineno"> 291</span><spanclass="comment">// These are the current tail/head of this edges. These are changed when</span></div>
<divclass="line"><aid="l00292"name="l00292"></a><spanclass="lineno"> 292</span><spanclass="comment">// creating or expanding blossoms. The order do not matter.</span></div>
<divclass="line"><aid="l00294"name="l00294"></a><spanclass="lineno"> 294</span><spanclass="comment">// TODO(user): Consider using node_a/node_b instead to remove the "directed"</span></div>
<divclass="line"><aid="l00295"name="l00295"></a><spanclass="lineno"> 295</span><spanclass="comment">// meaning. I do need to think a bit more about it though.</span></div>
<divclass="line"><aid="l00299"name="l00299"></a><spanclass="lineno"> 299</span><spanclass="comment">// Position of this Edge in the underlying std::vector<> used to encode the</span></div>
<divclass="line"><aid="l00300"name="l00300"></a><spanclass="lineno"> 300</span><spanclass="comment">// heap of one priority queue. An edge can be in at most one priority queue</span></div>
<divclass="line"><aid="l00301"name="l00301"></a><spanclass="lineno"> 301</span><spanclass="comment">// which allow us to share this amongst queues.</span></div>
<divclass="line"><aid="l00305"name="l00305"></a><spanclass="lineno"> 305</span><spanclass="comment">// Creates a BlossomGraph on the given number of nodes.</span></div>
<divclass="line"><aid="l00308"name="l00308"></a><spanclass="lineno"> 308</span><spanclass="comment">// Same comment as MinCostPerfectMatching::AddEdgeWithCost() applies.</span></div>
<divclass="line"><aid="l00311"name="l00311"></a><spanclass="lineno"> 311</span><spanclass="comment">// Heuristic to start with a dual-feasible solution and some matched edges.</span></div>
<divclass="line"><aid="l00312"name="l00312"></a><spanclass="lineno"> 312</span><spanclass="comment">// To be called once all edges are added. Returns false if the problem is</span></div>
<divclass="line"><aid="l00313"name="l00313"></a><spanclass="lineno"> 313</span><spanclass="comment">// detected to be INFEASIBLE.</span></div>
<divclass="line"><aid="l00316"name="l00316"></a><spanclass="lineno"> 316</span><spanclass="comment">// Enters a loop that perform one of Grow()/Augment()/Shrink()/Expand() until</span></div>
<divclass="line"><aid="l00317"name="l00317"></a><spanclass="lineno"> 317</span><spanclass="comment">// a fixed point is reached.</span></div>
<divclass="line"><aid="l00320"name="l00320"></a><spanclass="lineno"> 320</span><spanclass="comment">// Computes the maximum possible delta for UpdateAllTrees() that keeps the</span></div>
<divclass="line"><aid="l00321"name="l00321"></a><spanclass="lineno"> 321</span><spanclass="comment">// dual feasibility. Dual update approach (2) from the paper. This also fills</span></div>
<divclass="line"><aid="l00325"name="l00325"></a><spanclass="lineno"> 325</span><spanclass="comment">// Applies the same dual delta to all trees. Dual update approach (2) from the</span></div>
<divclass="line"><aid="l00329"name="l00329"></a><spanclass="lineno"> 329</span><spanclass="comment">// Returns true iff this node is matched and is thus not a tree root.</span></div>
<divclass="line"><aid="l00330"name="l00330"></a><spanclass="lineno"> 330</span><spanclass="comment">// This cannot live in the Node class because we need to know the NodeIndex.</span></div>
<divclass="line"><aid="l00333"name="l00333"></a><spanclass="lineno"> 333</span><spanclass="comment">// Returns the node matched to the given one, or n if this node is not</span></div>
<divclass="line"><aid="l00334"name="l00334"></a><spanclass="lineno"> 334</span><spanclass="comment">// currently matched.</span></div>
<divclass="line"><aid="l00337"name="l00337"></a><spanclass="lineno"> 337</span><spanclass="comment">// Adds to the tree of tail the free matched pair(head, Match(head)).</span></div>
<divclass="line"><aid="l00338"name="l00338"></a><spanclass="lineno"> 338</span><spanclass="comment">// The edge is only used in DCHECKs. We duplicate tail/head because the</span></div>
<divclass="line"><aid="l00339"name="l00339"></a><spanclass="lineno"> 339</span><spanclass="comment">// order matter here.</span></div>
<divclass="line"><aid="l00340"name="l00340"></a><spanclass="lineno"> 340</span><spanclass="keywordtype">void</span><aclass="code hl_function"href="classoperations__research_1_1_blossom_graph.html#a39b5c65490728705cd4471c549ff1d95">Grow</a>(EdgeIndex e, <aclass="code hl_typedef"href="namespaceoperations__research.html#a7ae31ba4c3b4899478e53ca13df35dfc">NodeIndex</a><aclass="code hl_variable"href="routing__flow_8cc.html#a64e7efc5529154ba56903e75f5300990">tail</a>, <aclass="code hl_typedef"href="namespaceoperations__research.html#a7ae31ba4c3b4899478e53ca13df35dfc">NodeIndex</a><aclass="code hl_variable"href="routing__flow_8cc.html#afca32f65388659a4b0956496169488b4">head</a>);</div>
<divclass="line"><aid="l00342"name="l00342"></a><spanclass="lineno"> 342</span><spanclass="comment">// Merges two tree and augment the number of matched nodes by 1. This is</span></div>
<divclass="line"><aid="l00343"name="l00343"></a><spanclass="lineno"> 343</span><spanclass="comment">// the only functions that change the current matching.</span></div>
<divclass="line"><aid="l00346"name="l00346"></a><spanclass="lineno"> 346</span><spanclass="comment">// Creates a Blossom using the given [+] -- [+] edge between two nodes of the</span></div>
<divclass="line"><aid="l00347"name="l00347"></a><spanclass="lineno"> 347</span><spanclass="comment">// same tree.</span></div>
<divclass="line"><aid="l00350"name="l00350"></a><spanclass="lineno"> 350</span><spanclass="comment">// Expands a Blossom into its component.</span></div>
<divclass="line"><aid="l00353"name="l00353"></a><spanclass="lineno"> 353</span><spanclass="comment">// Returns the current number of matched nodes.</span></div>
<divclass="line"><aid="l00356"name="l00356"></a><spanclass="lineno"> 356</span><spanclass="comment">// Returns the current dual objective which is always a valid lower-bound on</span></div>
<divclass="line"><aid="l00357"name="l00357"></a><spanclass="lineno"> 357</span><spanclass="comment">// the min-cost matching. Note that this is capped to kint64max in case of</span></div>
<divclass="line"><aid="l00358"name="l00358"></a><spanclass="lineno"> 358</span><spanclass="comment">// overflow. Because all of our cost are positive, this starts at zero.</span></div>
<divclass="line"><aid="l00361"name="l00361"></a><spanclass="lineno"> 361</span><spanclass="comment">// This must be called at the end of the algorithm to recover the matching.</span></div>
<divclass="line"><aid="l00364"name="l00364"></a><spanclass="lineno"> 364</span><spanclass="comment">// Return the "slack" of the given edge.</span></div>
<divclass="line"><aid="l00367"name="l00367"></a><spanclass="lineno"> 367</span><spanclass="comment">// Returns the dual value of the given node (which might be a pseudo-node).</span></div>
<divclass="line"><aid="l00370"name="l00370"></a><spanclass="lineno"> 370</span><spanclass="comment">// Display to VLOG(1) some statistic about the solve.</span></div>
<divclass="line"><aid="l00373"name="l00373"></a><spanclass="lineno"> 373</span><spanclass="comment">// Checks that there is no possible primal update in the current</span></div>
<divclass="line"><aid="l00377"name="l00377"></a><spanclass="lineno"> 377</span><spanclass="comment">// Tests that the dual values are currently feasible.</span></div>
<divclass="line"><aid="l00378"name="l00378"></a><spanclass="lineno"> 378</span><spanclass="comment">// This should ALWAYS be the case.</span></div>
<divclass="line"><aid="l00381"name="l00381"></a><spanclass="lineno"> 381</span><spanclass="comment">// In debug mode, we maintain the real slack of each edges and the real dual</span></div>
<divclass="line"><aid="l00382"name="l00382"></a><spanclass="lineno"> 382</span><spanclass="comment">// of each node via this function. Both Slack() and Dual() checks in debug</span></div>
<divclass="line"><aid="l00383"name="l00383"></a><spanclass="lineno"> 383</span><spanclass="comment">// mode that the value computed is the correct one.</span></div>
<divclass="line"><aid="l00384"name="l00384"></a><spanclass="lineno"> 384</span><spanclass="keywordtype">void</span><aclass="code hl_function"href="classoperations__research_1_1_blossom_graph.html#ad7e519048b150893f06581b7f270c118">DebugUpdateNodeDual</a>(<aclass="code hl_typedef"href="namespaceoperations__research.html#a7ae31ba4c3b4899478e53ca13df35dfc">NodeIndex</a> n, <aclass="code hl_typedef"href="namespaceoperations__research.html#aee97ac67f280d35acdef2c5d461a85c3">CostValue</a><aclass="code hl_variable"href="resource_8cc.html#af12056bc2a8e3ec563f0940d87abbd2e">delta</a>);</div>
<divclass="line"><aid="l00386"name="l00386"></a><spanclass="lineno"> 386</span><spanclass="comment">// Returns true iff this is an external edge with a slack of zero.</span></div>
<divclass="line"><aid="l00387"name="l00387"></a><spanclass="lineno"> 387</span><spanclass="comment">// An external edge is an edge between two external nodes.</span></div>
<divclass="line"><aid="l00390"name="l00390"></a><spanclass="lineno"> 390</span><spanclass="comment">// Getters to access node/edges from outside the class.</span></div>
<divclass="line"><aid="l00391"name="l00391"></a><spanclass="lineno"> 391</span><spanclass="comment">// Only used in tests.</span></div>
<divclass="line"><aid="l00401"name="l00401"></a><spanclass="lineno"> 401</span><spanclass="comment">// Returns the index of a tight edge between the two given external nodes.</span></div>
<divclass="line"><aid="l00402"name="l00402"></a><spanclass="lineno"> 402</span><spanclass="comment">// Returns kNoEdgeIndex if none could be found.</span></div>
<divclass="line"><aid="l00404"name="l00404"></a><spanclass="lineno"> 404</span><spanclass="comment">// TODO(user): Store edges for match/parent/blossom instead and remove the</span></div>
<divclass="line"><aid="l00405"name="l00405"></a><spanclass="lineno"> 405</span><spanclass="comment">// need for this function that can take around 10% of the running time on</span></div>
<divclass="line"><aid="l00406"name="l00406"></a><spanclass="lineno"> 406</span><spanclass="comment">// some problems.</span></div>
<divclass="line"><aid="l00409"name="l00409"></a><spanclass="lineno"> 409</span><spanclass="comment">// Appends the path from n to the root of its tree. Used by Augment().</span></div>
<divclass="line"><aid="l00410"name="l00410"></a><spanclass="lineno"> 410</span><spanclass="keywordtype">void</span> AppendNodePathToRoot(<aclass="code hl_typedef"href="namespaceoperations__research.html#a7ae31ba4c3b4899478e53ca13df35dfc">NodeIndex</a> n, std::vector<NodeIndex>* path) <spanclass="keyword">const</span>;</div>
<divclass="line"><aid="l00412"name="l00412"></a><spanclass="lineno"> 412</span><spanclass="comment">// Returns the depth of a node in its tree. Used by Shrink().</span></div>
<divclass="line"><aid="l00415"name="l00415"></a><spanclass="lineno"> 415</span><spanclass="comment">// Adds positive delta to dual_objective_ and cap at kint64max on overflow.</span></div>
<divclass="line"><aid="l00418"name="l00418"></a><spanclass="lineno"> 418</span><spanclass="comment">// In the presence of blossoms, the original tail/head of an arc might not be</span></div>
<divclass="line"><aid="l00419"name="l00419"></a><spanclass="lineno"> 419</span><spanclass="comment">// up to date anymore. It is important to use these functions instead in all</span></div>
<divclass="line"><aid="l00420"name="l00420"></a><spanclass="lineno"> 420</span><spanclass="comment">// the places where this can happen. That is basically everywhere except in</span></div>
<divclass="line"><aid="l00421"name="l00421"></a><spanclass="lineno"> 421</span><spanclass="comment">// the initialization.</span></div>
<divclass="line"><aid="l00429"name="l00429"></a><spanclass="lineno"> 429</span><spanclass="comment">// Returns the Head() or Tail() that does not correspond to node. Node that</span></div>
<divclass="line"><aid="l00430"name="l00430"></a><spanclass="lineno"> 430</span><spanclass="comment">// node must be one of the original index in the given edge, this is DCHECKed</span></div>
<divclass="line"><aid="l00431"name="l00431"></a><spanclass="lineno"> 431</span><spanclass="comment">// by edge.OtherEnd().</span></div>
<divclass="line"><aid="l00436"name="l00436"></a><spanclass="lineno"> 436</span><spanclass="comment">// Same as OtherEnd() but the given node should either be Tail(edge) or</span></div>
<divclass="line"><aid="l00437"name="l00437"></a><spanclass="lineno"> 437</span><spanclass="comment">// Head(edge) and do not need to be one of the original node of this edge.</span></div>
<divclass="line"><aid="l00447"name="l00447"></a><spanclass="lineno"> 447</span><spanclass="comment">// Returns the given node and if this node is a blossom, all its internal</span></div>
<divclass="line"><aid="l00448"name="l00448"></a><spanclass="lineno"> 448</span><spanclass="comment">// nodes (recursively). Note that any call to SubNodes() invalidate the</span></div>
<divclass="line"><aid="l00449"name="l00449"></a><spanclass="lineno"> 449</span><spanclass="comment">// previously returned reference.</span></div>
<divclass="line"><aid="l00452"name="l00452"></a><spanclass="lineno"> 452</span><spanclass="comment">// Just used to check that initialized is called exactly once.</span></div>
<divclass="line"><aid="l00455"name="l00455"></a><spanclass="lineno"> 455</span><spanclass="comment">// The set of all edges/nodes of the graph.</span></div>
<divclass="line"><aid="l00459"name="l00459"></a><spanclass="lineno"> 459</span><spanclass="comment">// Identity for a non-blossom node, and its top blossom node (in case of many</span></div>
<divclass="line"><aid="l00460"name="l00460"></a><spanclass="lineno"> 460</span><spanclass="comment">// nested blossom) for an internal node.</span></div>
<divclass="line"><aid="l00463"name="l00463"></a><spanclass="lineno"> 463</span><spanclass="comment">// The current graph incidence. Note that one EdgeIndex should appear in</span></div>
<divclass="line"><aid="l00464"name="l00464"></a><spanclass="lineno"> 464</span><spanclass="comment">// exactly two places (on its tail and head incidence list).</span></div>
<divclass="line"><aid="l00470"name="l00470"></a><spanclass="lineno"> 470</span><spanclass="comment">// The unmatched nodes are exactly the root of the trees. After</span></div>
<divclass="line"><aid="l00471"name="l00471"></a><spanclass="lineno"> 471</span><spanclass="comment">// initialization, this is only modified by Augment() which removes two nodes</span></div>
<divclass="line"><aid="l00472"name="l00472"></a><spanclass="lineno"> 472</span><spanclass="comment">// from this list each time. Note that during Shrink()/Expand() we never</span></div>
<divclass="line"><aid="l00473"name="l00473"></a><spanclass="lineno"> 473</span><spanclass="comment">// change the indexing of the root nodes.</span></div>
<divclass="line"><aid="l00476"name="l00476"></a><spanclass="lineno"> 476</span><spanclass="comment">// List of tight_edges and possible shrink to check in PrimalUpdates().</span></div>
<divclass="line"><aid="l00480"name="l00480"></a><spanclass="lineno"> 480</span><spanclass="comment">// Priority queues of edges of a certain types.</span></div>
<divclass="line"><aid="l00485"name="l00485"></a><spanclass="lineno"> 485</span><spanclass="comment">// The dual objective. Increase as the algorithm progress. This is a lower</span></div>
<divclass="line"><aid="l00486"name="l00486"></a><spanclass="lineno"> 486</span><spanclass="comment">// bound on the min-cost of a perfect matching.</span></div>
<divclass="ttc"id="aclassoperations__research_1_1_blossom_graph_html_ad7e519048b150893f06581b7f270c118"><divclass="ttname"><ahref="classoperations__research_1_1_blossom_graph.html#ad7e519048b150893f06581b7f270c118">operations_research::BlossomGraph::DebugUpdateNodeDual</a></div><divclass="ttdeci">void DebugUpdateNodeDual(NodeIndex n, CostValue delta)</div><divclass="ttdef"><b>Definition:</b><ahref="perfect__matching_8cc_source.html#l01218">perfect_matching.cc:1218</a></div></div>
<divclass="ttc"id="aclassoperations__research_1_1_min_cost_perfect_matching_html_a00a07ecd49cf20c2806ebbcd1f5dcbb1"><divclass="ttname"><ahref="classoperations__research_1_1_min_cost_perfect_matching.html#a00a07ecd49cf20c2806ebbcd1f5dcbb1">operations_research::MinCostPerfectMatching::Solve</a></div><divclass="ttdeci">ABSL_MUST_USE_RESULT Status Solve()</div><divclass="ttdef"><b>Definition:</b><ahref="perfect__matching_8cc_source.html#l00042">perfect_matching.cc:42</a></div></div>
<divclass="ttc"id="aclassoperations__research_1_1_min_cost_perfect_matching_html_a132e58de7ebb46a1ba677b77f6e60907"><divclass="ttname"><ahref="classoperations__research_1_1_min_cost_perfect_matching.html#a132e58de7ebb46a1ba677b77f6e60907">operations_research::MinCostPerfectMatching::AddEdgeWithCost</a></div><divclass="ttdeci">void AddEdgeWithCost(int tail, int head, int64_t cost)</div><divclass="ttdef"><b>Definition:</b><ahref="perfect__matching_8cc_source.html#l00030">perfect_matching.cc:30</a></div></div>
<divclass="ttc"id="anamespaceoperations__research_html"><divclass="ttname"><ahref="namespaceoperations__research.html">operations_research</a></div><divclass="ttdoc">Collection of objects used to extend the Constraint Solver library.</div><divclass="ttdef"><b>Definition:</b><ahref="dense__doubly__linked__list_8h_source.html#l00021">dense_doubly_linked_list.h:21</a></div></div>