<ahref="topologicalsorter_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">// TopologicalSorter provides topologically sorted traversal of the</span></div>
<divclass="line"><aid="l00015"name="l00015"></a><spanclass="lineno"> 15</span><spanclass="comment">// nodes of a directed acyclic graph (DAG) with up to INT_MAX nodes.</span></div>
<divclass="line"><aid="l00016"name="l00016"></a><spanclass="lineno"> 16</span><spanclass="comment">// It sorts ancestor nodes before their descendants.</span></div>
<divclass="line"><aid="l00018"name="l00018"></a><spanclass="lineno"> 18</span><spanclass="comment">// If your graph is not a DAG and you're reading this, you are probably</span></div>
<divclass="line"><aid="l00019"name="l00019"></a><spanclass="lineno"> 19</span><spanclass="comment">// looking for ortools/graph/strongly_connected_components.h which does</span></div>
<divclass="line"><aid="l00020"name="l00020"></a><spanclass="lineno"> 20</span><spanclass="comment">// the topological decomposition of a directed graph.</span></div>
<divclass="line"><aid="l00027"name="l00027"></a><spanclass="lineno"> 27</span><spanclass="comment">// if (util::StableTopologicalSort(num_nodes, arcs, &result)) {</span></div>
<divclass="line"><aid="l00028"name="l00028"></a><spanclass="lineno"> 28</span><spanclass="comment">// LOG(INFO) <<"The topological order is: "<< gtl::LogContainer(result);</span></div>
<divclass="line"><aid="l00030"name="l00030"></a><spanclass="lineno"> 30</span><spanclass="comment">// LOG(INFO) <<"The graph is cyclic.";</span></div>
<divclass="line"><aid="l00031"name="l00031"></a><spanclass="lineno"> 31</span><spanclass="comment">// // Note: you can extract a cycle with the TopologicalSorter class, or</span></div>
<divclass="line"><aid="l00032"name="l00032"></a><spanclass="lineno"> 32</span><spanclass="comment">// // with the API defined in circularity_detector.h.</span></div>
<divclass="line"><aid="l00034"name="l00034"></a><spanclass="lineno"> 34</span><spanclass="comment">// // This will be successful and result will be equal to {"a", "b", "c"}.</span></div>
<divclass="line"><aid="l00036"name="l00036"></a><spanclass="lineno"> 36</span><spanclass="comment">// There are 8 flavors of topological sort, from these 3 bits:</span></div>
<divclass="line"><aid="l00037"name="l00037"></a><spanclass="lineno"> 37</span><spanclass="comment">// - There are OrDie() versions that directly return the topological order, or</span></div>
<divclass="line"><aid="l00038"name="l00038"></a><spanclass="lineno"> 38</span><spanclass="comment">// crash if a cycle is detected (and LOG the cycle).</span></div>
<divclass="line"><aid="l00039"name="l00039"></a><spanclass="lineno"> 39</span><spanclass="comment">// - There are type-generic versions that can take any node type (including</span></div>
<divclass="line"><aid="l00040"name="l00040"></a><spanclass="lineno"> 40</span><spanclass="comment">// non-dense integers), but slower, or the "dense int" versions which requires</span></div>
<divclass="line"><aid="l00041"name="l00041"></a><spanclass="lineno"> 41</span><spanclass="comment">// nodes to be a dense interval [0..num_nodes-1]. Note that the type must</span></div>
<divclass="line"><aid="l00042"name="l00042"></a><spanclass="lineno"> 42</span><spanclass="comment">// be compatible with LOG << T if you're using the OrDie() version.</span></div>
<divclass="line"><aid="l00043"name="l00043"></a><spanclass="lineno"> 43</span><spanclass="comment">// - The sorting can be either stable or not. "Stable" essentially means that it</span></div>
<divclass="line"><aid="l00044"name="l00044"></a><spanclass="lineno"> 44</span><spanclass="comment">// will preserve the order of nodes, if possible. More precisely, the returned</span></div>
<divclass="line"><aid="l00045"name="l00045"></a><spanclass="lineno"> 45</span><spanclass="comment">// topological order will be the lexicographically minimal valid order, where</span></div>
<divclass="line"><aid="l00046"name="l00046"></a><spanclass="lineno"> 46</span><spanclass="comment">// "lexicographic" applies to the indices of the nodes.</span></div>
<divclass="line"><aid="l00057"name="l00057"></a><spanclass="lineno"> 57</span><spanclass="comment">// If you need more control, or a step-by-step topological sort, see the</span></div>
<divclass="line"><aid="l00077"name="l00077"></a><spanclass="lineno"> 77</span><spanclass="comment">// Returns true if the graph was a DAG, and outputs the topological order in</span></div>
<divclass="line"><aid="l00078"name="l00078"></a><spanclass="lineno"> 78</span><spanclass="comment">// "topological_order". Returns false if the graph is cyclic.</span></div>
<divclass="line"><aid="l00079"name="l00079"></a><spanclass="lineno"> 79</span><spanclass="comment">// Works in O(num_nodes + arcs.size()), and is pretty fast.</span></div>
<divclass="line"><aid="l00084"name="l00084"></a><spanclass="lineno"> 84</span><spanclass="comment">// Like DenseIntTopologicalSort, but stable.</span></div>
<divclass="line"><aid="l00089"name="l00089"></a><spanclass="lineno"> 89</span><spanclass="comment">// Finds a cycle in the directed graph given as argument: nodes are dense</span></div>
<divclass="line"><aid="l00090"name="l00090"></a><spanclass="lineno"> 90</span><spanclass="comment">// integers in 0..num_nodes-1, and (directed) arcs are pairs of nodes</span></div>
<divclass="line"><aid="l00092"name="l00092"></a><spanclass="lineno"> 92</span><spanclass="comment">// The returned cycle is a list of nodes that form a cycle, eg. {1, 4, 3}</span></div>
<divclass="line"><aid="l00093"name="l00093"></a><spanclass="lineno"> 93</span><spanclass="comment">// if the cycle 1->4->3->1 exists.</span></div>
<divclass="line"><aid="l00094"name="l00094"></a><spanclass="lineno"> 94</span><spanclass="comment">// If the graph is acyclic, returns an empty vector.</span></div>
<divclass="line"><aid="l00098"name="l00098"></a><spanclass="lineno"> 98</span><spanclass="comment">// Like the two above, but with generic node types. The nodes must be provided.</span></div>
<divclass="line"><aid="l00099"name="l00099"></a><spanclass="lineno"> 99</span><spanclass="comment">// Can be significantly slower, but still linear.</span></div>
<divclass="line"><aid="l00109"name="l00109"></a><spanclass="lineno"> 109</span><spanclass="comment">// "OrDie()" versions of the 4 functions above. Those directly return the</span></div>
<divclass="line"><aid="l00110"name="l00110"></a><spanclass="lineno"> 110</span><spanclass="comment">// topological order, which makes their API even simpler.</span></div>
<divclass="line"><aid="l00123"name="l00123"></a><spanclass="lineno"> 123</span><spanclass="comment">// Internal wrapper around the *TopologicalSort classes.</span></div>
<divclass="line"><aid="l00129"name="l00129"></a><spanclass="lineno"> 129</span><spanclass="comment">// Do not use the templated class directly, instead use one of the</span></div>
<divclass="line"><aid="l00130"name="l00130"></a><spanclass="lineno"> 130</span><spanclass="comment">// typedefs DenseIntTopologicalSorter or DenseIntStableTopologicalSorter.</span></div>
<divclass="line"><aid="l00132"name="l00132"></a><spanclass="lineno"> 132</span><spanclass="comment">// The equivalent of a TopologicalSorter<int> which nodes are the</span></div>
<divclass="line"><aid="l00133"name="l00133"></a><spanclass="lineno"> 133</span><spanclass="comment">// N integers from 0 to N-1 (see the toplevel comment). The API is</span></div>
<divclass="line"><aid="l00134"name="l00134"></a><spanclass="lineno"> 134</span><spanclass="comment">// exactly similar to that of TopologicalSorter, please refer to the</span></div>
<divclass="line"><aid="l00135"name="l00135"></a><spanclass="lineno"> 135</span><spanclass="comment">// TopologicalSorter class below for more detailed comments.</span></div>
<divclass="line"><aid="l00137"name="l00137"></a><spanclass="lineno"> 137</span><spanclass="comment">// If the template parameter is true then the sort will be stable.</span></div>
<divclass="line"><aid="l00138"name="l00138"></a><spanclass="lineno"> 138</span><spanclass="comment">// This means that the order of the nodes will be maintained as much as</span></div>
<divclass="line"><aid="l00139"name="l00139"></a><spanclass="lineno"> 139</span><spanclass="comment">// possible. A non-stable sort is more efficient, since the complexity</span></div>
<divclass="line"><aid="l00140"name="l00140"></a><spanclass="lineno"> 140</span><spanclass="comment">// of getting the next node is O(1) rather than O(log(Nodes)).</span></div>
<divclass="line"><aid="l00144"name="l00144"></a><spanclass="lineno"> 144</span><spanclass="comment">// To store the adjacency lists efficiently.</span></div>
<divclass="line"><aid="l00147"name="l00147"></a><spanclass="lineno"> 147</span><spanclass="comment">// For efficiency, it is best to specify how many nodes are required</span></div>
<divclass="line"><aid="l00148"name="l00148"></a><spanclass="lineno"> 148</span><spanclass="comment">// by using the next constructor.</span></div>
<divclass="line"><aid="l00154"name="l00154"></a><spanclass="lineno"> 154</span><spanclass="comment">// One may also construct a DenseIntTopologicalSorterTpl with a predefined</span></div>
<divclass="line"><aid="l00155"name="l00155"></a><spanclass="lineno"> 155</span><spanclass="comment">// number of empty nodes. One can thus bypass the AddNode() API,</span></div>
<divclass="line"><aid="l00156"name="l00156"></a><spanclass="lineno"> 156</span><spanclass="comment">// which may yield a lower memory usage.</span></div>
<divclass="line"><aid="l00163"name="l00163"></a><spanclass="lineno"> 163</span><spanclass="comment">// Performs in constant amortized time. Calling this will make all</span></div>
<divclass="line"><aid="l00164"name="l00164"></a><spanclass="lineno"> 164</span><spanclass="comment">// node indices in [0 .. node_index] be valid node indices. If you</span></div>
<divclass="line"><aid="l00165"name="l00165"></a><spanclass="lineno"> 165</span><spanclass="comment">// can avoid using AddNode(), you should! If you know the number of</span></div>
<divclass="line"><aid="l00166"name="l00166"></a><spanclass="lineno"> 166</span><spanclass="comment">// nodes in advance, you should specify that at construction time --</span></div>
<divclass="line"><aid="l00167"name="l00167"></a><spanclass="lineno"> 167</span><spanclass="comment">// it will be faster and use less memory.</span></div>
<divclass="line"><aid="l00170"name="l00170"></a><spanclass="lineno"> 170</span><spanclass="comment">// Performs in constant amortized time. Calling this will make all</span></div>
<divclass="line"><aid="l00171"name="l00171"></a><spanclass="lineno"> 171</span><spanclass="comment">// node indices in [0, max(from, to)] be valid node indices.</span></div>
<divclass="line"><aid="l00174"name="l00174"></a><spanclass="lineno"> 174</span><spanclass="comment">// Performs in O(average degree) in average. If a cycle is detected</span></div>
<divclass="line"><aid="l00175"name="l00175"></a><spanclass="lineno"> 175</span><spanclass="comment">// and "output_cycle_nodes" isn't NULL, it will require an additional</span></div>
<divclass="line"><aid="l00176"name="l00176"></a><spanclass="lineno"> 176</span><spanclass="comment">// O(number of edges + number of nodes in the graph) time.</span></div>
<divclass="line"><aid="l00189"name="l00189"></a><spanclass="lineno"> 189</span><spanclass="comment">// Given a vector<AdjacencyList> of size n such that elements of the</span></div>
<divclass="line"><aid="l00190"name="l00190"></a><spanclass="lineno"> 190</span><spanclass="comment">// AdjacencyList are in [0, n-1], remove the duplicates within each</span></div>
<divclass="line"><aid="l00191"name="l00191"></a><spanclass="lineno"> 191</span><spanclass="comment">// AdjacencyList of size greater or equal to skip_lists_smaller_than,</span></div>
<divclass="line"><aid="l00192"name="l00192"></a><spanclass="lineno"> 192</span><spanclass="comment">// in linear time. Returns the total number of duplicates removed.</span></div>
<divclass="line"><aid="l00193"name="l00193"></a><spanclass="lineno"> 193</span><spanclass="comment">// This method is exposed for unit testing purposes only.</span></div>
<divclass="line"><aid="l00197"name="l00197"></a><spanclass="lineno"> 197</span><spanclass="comment">// To extract a cycle. When there is no cycle, cycle_nodes will be empty.</span></div>
<divclass="line"><aid="l00206"name="l00206"></a><spanclass="lineno"> 206</span><spanclass="comment">// Only valid after a traversal started.</span></div>
<divclass="line"><aid="l00210"name="l00210"></a><spanclass="lineno"> 210</span><spanclass="comment">// We use greater<int> so that the lowest elements gets popped first.</span></div>
<divclass="line"><aid="l00215"name="l00215"></a><spanclass="lineno"> 215</span><spanclass="comment">// Used internally by AddEdge() to decide whether to trigger</span></div>
<divclass="line"><aid="l00216"name="l00216"></a><spanclass="lineno"> 216</span><spanclass="comment">// RemoveDuplicates(). See the .cc.</span></div>
<divclass="line"><aid="l00217"name="l00217"></a><spanclass="lineno"> 217</span><spanclass="keywordtype">int</span> num_edges_; <spanclass="comment">// current total number of edges.</span></div>
<divclass="line"><aid="l00229"name="l00229"></a><spanclass="lineno"> 229</span><spanclass="comment">// Recommended version for general usage. The stability makes it more</span></div>
<divclass="line"><aid="l00230"name="l00230"></a><spanclass="lineno"> 230</span><spanclass="comment">// deterministic, and its behavior is guaranteed to never change.</span></div>
<divclass="line"><aid="l00235"name="l00235"></a><spanclass="lineno"> 235</span><spanclass="comment">// Use this version if you are certain you don't care about the</span></div>
<divclass="line"><aid="l00236"name="l00236"></a><spanclass="lineno"> 236</span><spanclass="comment">// tie-breaking order and need the 5 to 10% performance gain. The</span></div>
<divclass="line"><aid="l00237"name="l00237"></a><spanclass="lineno"> 237</span><spanclass="comment">// performance gain can be more significant for large graphs with large</span></div>
<divclass="line"><aid="l00238"name="l00238"></a><spanclass="lineno"> 238</span><spanclass="comment">// numbers of source nodes (for example 2 Million nodes with 2 Million</span></div>
<divclass="line"><aid="l00239"name="l00239"></a><spanclass="lineno"> 239</span><spanclass="comment">// random edges sees a factor of 0.7 difference in completion time).</span></div>
<divclass="line"><aid="l00244"name="l00244"></a><spanclass="lineno"> 244</span><spanclass="comment">// A copy of each Node is stored internally. Duplicated edges are allowed,</span></div>
<divclass="line"><aid="l00245"name="l00245"></a><spanclass="lineno"> 245</span><spanclass="comment">// and discarded lazily so that AddEdge() keeps an amortized constant</span></div>
<divclass="line"><aid="l00246"name="l00246"></a><spanclass="lineno"> 246</span><spanclass="comment">// time, yet the total memory usage remains O(number of different edges +</span></div>
<divclass="line"><aid="l00247"name="l00247"></a><spanclass="lineno"> 247</span><spanclass="comment">// number of nodes).</span></div>
<divclass="line"><aid="l00249"name="l00249"></a><spanclass="lineno"> 249</span><spanclass="comment">// DenseIntTopologicalSorter implements the core topological sort</span></div>
<divclass="line"><aid="l00250"name="l00250"></a><spanclass="lineno"> 250</span><spanclass="comment">// algorithm. For greater efficiency it can be used directly</span></div>
<divclass="line"><aid="l00251"name="l00251"></a><spanclass="lineno"> 251</span><spanclass="comment">// (TopologicalSorter<int> is about 1.5-3x slower).</span></div>
<divclass="line"><aid="l00253"name="l00253"></a><spanclass="lineno"> 253</span><spanclass="comment">// TopologicalSorter requires that all nodes and edges be added before</span></div>
<divclass="line"><aid="l00254"name="l00254"></a><spanclass="lineno"> 254</span><spanclass="comment">// traversing the nodes, otherwise it will die with a fatal error.</span></div>
<divclass="line"><aid="l00257"name="l00257"></a><spanclass="lineno"> 257</span><spanclass="comment">// Note(user): since all the real work is done by</span></div>
<divclass="line"><aid="l00258"name="l00258"></a><spanclass="lineno"> 258</span><spanclass="comment">// DenseIntTopologicalSorterTpl, and this class is a template, we inline</span></div>
<divclass="line"><aid="l00259"name="l00259"></a><spanclass="lineno"> 259</span><spanclass="comment">// every function here in the .h.</span></div>
<divclass="line"><aid="l00261"name="l00261"></a><spanclass="lineno"> 261</span><spanclass="comment">// If stable_sort is true then the topological sort will preserve the</span></div>
<divclass="line"><aid="l00262"name="l00262"></a><spanclass="lineno"> 262</span><spanclass="comment">// original order of the nodes as much as possible. Note, the order</span></div>
<divclass="line"><aid="l00263"name="l00263"></a><spanclass="lineno"> 263</span><spanclass="comment">// which is preserved is the order in which the nodes are added (if you</span></div>
<divclass="line"><aid="l00264"name="l00264"></a><spanclass="lineno"> 264</span><spanclass="comment">// use AddEdge it will add the first argument and then the second).</span></div>
<divclass="line"><aid="l00274"name="l00274"></a><spanclass="lineno"> 274</span><spanclass="comment">// Adds a node to the graph, if it has not already been added via</span></div>
<divclass="line"><aid="l00275"name="l00275"></a><spanclass="lineno"> 275</span><spanclass="comment">// previous calls to AddNode()/AddEdge(). If no edges are later</span></div>
<divclass="line"><aid="l00276"name="l00276"></a><spanclass="lineno"> 276</span><spanclass="comment">// added connecting this node, then it remains an isolated node in</span></div>
<divclass="line"><aid="l00277"name="l00277"></a><spanclass="lineno"> 277</span><spanclass="comment">// the graph. AddNode() only exists to support isolated nodes. There</span></div>
<divclass="line"><aid="l00278"name="l00278"></a><spanclass="lineno"> 278</span><spanclass="comment">// is no requirement (nor is it an error) to call AddNode() for the</span></div>
<divclass="line"><aid="l00279"name="l00279"></a><spanclass="lineno"> 279</span><spanclass="comment">// endpoints used in a call to AddEdge(). Dies with a fatal error if</span></div>
<divclass="line"><aid="l00280"name="l00280"></a><spanclass="lineno"> 280</span><spanclass="comment">// called after a traversal has been started (see TraversalStarted()),</span></div>
<divclass="line"><aid="l00281"name="l00281"></a><spanclass="lineno"> 281</span><spanclass="comment">// or if more than INT_MAX nodes are being added.</span></div>
<divclass="line"><aid="l00284"name="l00284"></a><spanclass="lineno"> 284</span><spanclass="comment">// Adds a directed edge with the given endpoints to the graph. There</span></div>
<divclass="line"><aid="l00285"name="l00285"></a><spanclass="lineno"> 285</span><spanclass="comment">// is no requirement (nor is it an error) to call AddNode() for the</span></div>
<divclass="line"><aid="l00286"name="l00286"></a><spanclass="lineno"> 286</span><spanclass="comment">// endpoints. Dies with a fatal error if called after a traversal</span></div>
<divclass="line"><aid="l00287"name="l00287"></a><spanclass="lineno"> 287</span><spanclass="comment">// has been started (see TraversalStarted()).</span></div>
<divclass="line"><aid="l00289"name="l00289"></a><spanclass="lineno"> 289</span><spanclass="comment">// The lookups are not inlined into AddEdge because we need to ensure that</span></div>
<divclass="line"><aid="l00290"name="l00290"></a><spanclass="lineno"> 290</span><spanclass="comment">// "from" is inserted before "to".</span></div>
<divclass="line"><aid="l00296"name="l00296"></a><spanclass="lineno"> 296</span><spanclass="comment">// Visits the least node in topological order over the current set of</span></div>
<divclass="line"><aid="l00297"name="l00297"></a><spanclass="lineno"> 297</span><spanclass="comment">// nodes and edges, and marks that node as visited, so that repeated</span></div>
<divclass="line"><aid="l00298"name="l00298"></a><spanclass="lineno"> 298</span><spanclass="comment">// calls to GetNext() will visit all nodes in order. Writes the newly</span></div>
<divclass="line"><aid="l00299"name="l00299"></a><spanclass="lineno"> 299</span><spanclass="comment">// visited node in *node and returns true with *cyclic set to false</span></div>
<divclass="line"><aid="l00300"name="l00300"></a><spanclass="lineno"> 300</span><spanclass="comment">// (assuming the graph has not yet been discovered to be cyclic).</span></div>
<divclass="line"><aid="l00301"name="l00301"></a><spanclass="lineno"> 301</span><spanclass="comment">// Returns false if all nodes have been visited, or if the graph is</span></div>
<divclass="line"><aid="l00302"name="l00302"></a><spanclass="lineno"> 302</span><spanclass="comment">// discovered to be cyclic, in which case *cyclic is also set to true.</span></div>
<divclass="line"><aid="l00304"name="l00304"></a><spanclass="lineno"> 304</span><spanclass="comment">// If you set the optional argument "output_cycle_nodes" to non-NULL and</span></div>
<divclass="line"><aid="l00305"name="l00305"></a><spanclass="lineno"> 305</span><spanclass="comment">// a cycle is detected, it will dump an arbitrary cycle of the graph</span></div>
<divclass="line"><aid="l00306"name="l00306"></a><spanclass="lineno"> 306</span><spanclass="comment">// (whose length will be between 1 and #number_of_nodes, inclusive),</span></div>
<divclass="line"><aid="l00307"name="l00307"></a><spanclass="lineno"> 307</span><spanclass="comment">// in the natural order: for example if "output_cycle_nodes" is filled</span></div>
<divclass="line"><aid="l00308"name="l00308"></a><spanclass="lineno"> 308</span><spanclass="comment">// with ["A", "C", "B"], it means that A->C->B->A is a directed cycle</span></div>
<divclass="line"><aid="l00309"name="l00309"></a><spanclass="lineno"> 309</span><spanclass="comment">// of the graph.</span></div>
<divclass="line"><aid="l00311"name="l00311"></a><spanclass="lineno"> 311</span><spanclass="comment">// This starts a traversal (if not started already). Note that the</span></div>
<divclass="line"><aid="l00312"name="l00312"></a><spanclass="lineno"> 312</span><spanclass="comment">// graph can only be traversed once.</span></div>
<divclass="line"><aid="l00332"name="l00332"></a><spanclass="lineno"> 332</span><spanclass="comment">// Returns the number of nodes that currently have zero indegree.</span></div>
<divclass="line"><aid="l00333"name="l00333"></a><spanclass="lineno"> 333</span><spanclass="comment">// This starts a traversal (if not started already).</span></div>
<divclass="line"><aid="l00339"name="l00339"></a><spanclass="lineno"> 339</span><spanclass="comment">// Start a traversal. See TraversalStarted(). This initializes the</span></div>
<divclass="line"><aid="l00340"name="l00340"></a><spanclass="lineno"> 340</span><spanclass="comment">// various data structures of the sorter. Since this takes O(num_nodes</span></div>
<divclass="line"><aid="l00341"name="l00341"></a><spanclass="lineno"> 341</span><spanclass="comment">// + num_edges) time, users may want to call this at their convenience,</span></div>
<divclass="line"><aid="l00342"name="l00342"></a><spanclass="lineno"> 342</span><spanclass="comment">// instead of making it happen with the first GetNext().</span></div>
<divclass="line"><aid="l00346"name="l00346"></a><spanclass="lineno"> 346</span><spanclass="comment">// We move elements from the absl::flat_hash_map to this vector, without</span></div>
<divclass="line"><aid="l00347"name="l00347"></a><spanclass="lineno"> 347</span><spanclass="comment">// extra copy (if they are movable).</span></div>
<divclass="line"><aid="l00355"name="l00355"></a><spanclass="lineno"> 355</span><spanclass="comment">// Whether a traversal has started. If true, AddNode() and AddEdge()</span></div>
<divclass="line"><aid="l00356"name="l00356"></a><spanclass="lineno"> 356</span><spanclass="comment">// can no longer be called.</span></div>
<divclass="line"><aid="l00360"name="l00360"></a><spanclass="lineno"> 360</span><spanclass="comment">// A simple mapping from node to their dense index, in 0..num_nodes-1,</span></div>
<divclass="line"><aid="l00361"name="l00361"></a><spanclass="lineno"> 361</span><spanclass="comment">// which will be their index in nodes_[]. Cleared when a traversal</span></div>
<divclass="line"><aid="l00362"name="l00362"></a><spanclass="lineno"> 362</span><spanclass="comment">// starts, and replaced by nodes_[].</span></div>
<divclass="line"><aid="l00365"name="l00365"></a><spanclass="lineno"> 365</span><spanclass="comment">// Stores all the nodes as soon as a traversal starts.</span></div>
<divclass="line"><aid="l00368"name="l00368"></a><spanclass="lineno"> 368</span><spanclass="comment">// An internal DenseIntTopologicalSorterTpl that does all the real work.</span></div>
<divclass="line"><aid="l00371"name="l00371"></a><spanclass="lineno"> 371</span><spanclass="comment">// Used internally to extract cycles from the underlying</span></div>
<divclass="line"><aid="l00375"name="l00375"></a><spanclass="lineno"> 375</span><spanclass="comment">// Lookup an existing node's index, or add the node and return the</span></div>
<divclass="line"><aid="l00376"name="l00376"></a><spanclass="lineno"> 376</span><spanclass="comment">// new index that was assigned to it.</span></div>
<divclass="line"><aid="l00385"name="l00385"></a><spanclass="lineno"> 385</span><spanclass="comment">// If successful, returns true and outputs the order in "topological_order".</span></div>
<divclass="line"><aid="l00386"name="l00386"></a><spanclass="lineno"> 386</span><spanclass="comment">// If not, returns false and outputs a cycle in "cycle" (if not null).</span></div>
<divclass="line"><aid="l00397"name="l00397"></a><spanclass="lineno"> 397</span> T <aclass="code hl_variable"href="constraint__solver_8cc.html#a395f613555f398dd389670bb4c2a4599">next</a>;</div>
<divclass="line"><aid="l00425"name="l00425"></a><spanclass="lineno"> 425</span><spanclass="comment">// Now, the OrDie() versions, which directly return the topological order.</span></div>
<divclass="line"><aid="l00453"name="l00453"></a><spanclass="lineno"> 453</span><spanclass="comment">// Implementations of the "simple API" functions declared at the top.</span></div>
<divclass="line"><aid="l00508"name="l00508"></a><spanclass="lineno"> 508</span><spanclass="comment">// Some of the classes or functions have been exposed under the global namespace</span></div>
<divclass="line"><aid="l00509"name="l00509"></a><spanclass="lineno"> 509</span><spanclass="comment">// or the util::graph:: namespace. Until all clients are fixed to use the</span></div>
<divclass="line"><aid="l00510"name="l00510"></a><spanclass="lineno"> 510</span><spanclass="comment">// util:: namespace, we keep those versions around.</span></div>
<divclass="ttc"id="aclassutil_1_1_topological_sorter_html_a038de061979331bea01ffa96da55fac2"><divclass="ttname"><ahref="classutil_1_1_topological_sorter.html#a038de061979331bea01ffa96da55fac2">util::TopologicalSorter::AddEdge</a></div><divclass="ttdeci">void AddEdge(const T &from, const T &to)</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8h_source.html#l00288">topologicalsorter.h:288</a></div></div>
<divclass="ttc"id="aclassutil_1_1_topological_sorter_html_adf35867ff0932290f5456b27d1ed6bff"><divclass="ttname"><ahref="classutil_1_1_topological_sorter.html#adf35867ff0932290f5456b27d1ed6bff">util::TopologicalSorter::AddNode</a></div><divclass="ttdeci">void AddNode(const T &node)</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8h_source.html#l00282">topologicalsorter.h:282</a></div></div>
<divclass="ttc"id="aclassutil_1_1internal_1_1_dense_int_topological_sorter_tpl_html_a07aa254fe7a666f42819636a91cea372"><divclass="ttname"><ahref="classutil_1_1internal_1_1_dense_int_topological_sorter_tpl.html#a07aa254fe7a666f42819636a91cea372">util::internal::DenseIntTopologicalSorterTpl::ExtractCycle</a></div><divclass="ttdeci">void ExtractCycle(std::vector< int > *cycle_nodes) const</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8cc_source.html#l00220">topologicalsorter.cc:220</a></div></div>
<divclass="ttc"id="aclassutil_1_1internal_1_1_dense_int_topological_sorter_tpl_html_a0c4392b8dbb1d4677e6d1a1b52bb9898"><divclass="ttname"><ahref="classutil_1_1internal_1_1_dense_int_topological_sorter_tpl.html#a0c4392b8dbb1d4677e6d1a1b52bb9898">util::internal::DenseIntTopologicalSorterTpl::AddEdge</a></div><divclass="ttdeci">void AddEdge(int from, int to)</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8cc_source.html#l00065">topologicalsorter.cc:65</a></div></div>
<divclass="ttc"id="aclassutil_1_1internal_1_1_dense_int_topological_sorter_tpl_html_a86eb0e226467f5aa86a6212fe900a44f"><divclass="ttname"><ahref="classutil_1_1internal_1_1_dense_int_topological_sorter_tpl.html#a86eb0e226467f5aa86a6212fe900a44f">util::internal::DenseIntTopologicalSorterTpl::RemoveDuplicates</a></div><divclass="ttdeci">static int RemoveDuplicates(std::vector< AdjacencyList > *lists, int skip_lists_smaller_than)</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8cc_source.html#l00170">topologicalsorter.cc:170</a></div></div>
<divclass="ttc"id="aclassutil_1_1internal_1_1_dense_int_topological_sorter_tpl_html_aae2aec5b86307e8702d0a3fdd74f9be2"><divclass="ttname"><ahref="classutil_1_1internal_1_1_dense_int_topological_sorter_tpl.html#aae2aec5b86307e8702d0a3fdd74f9be2">util::internal::DenseIntTopologicalSorterTpl::AdjacencyList</a></div><divclass="ttdeci">std::vector< int > AdjacencyList</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8h_source.html#l00145">topologicalsorter.h:145</a></div></div>
<divclass="ttc"id="anamespaceutil_1_1graph_html_a1b429182836eddf34189e5af39f1e886"><divclass="ttname"><ahref="namespaceutil_1_1graph.html#a1b429182836eddf34189e5af39f1e886">util::graph::DenseIntStableTopologicalSortOrDie</a></div><divclass="ttdeci">std::vector< int > DenseIntStableTopologicalSortOrDie(int num_nodes, const std::vector< std::pair< int, int >>&arcs)</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8h_source.html#l00526">topologicalsorter.h:526</a></div></div>
<divclass="ttc"id="anamespaceutil_1_1graph_html_a4bcd58ffa60a8a68fb960ff41deba777"><divclass="ttname"><ahref="namespaceutil_1_1graph.html#a4bcd58ffa60a8a68fb960ff41deba777">util::graph::StableTopologicalSortOrDie</a></div><divclass="ttdeci">std::vector< T > StableTopologicalSortOrDie(const std::vector< T >&nodes, const std::vector< std::pair< T, T >>&arcs)</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8h_source.html#l00531">topologicalsorter.h:531</a></div></div>
<divclass="ttc"id="anamespaceutil_1_1graph_html_a5c544f7c7c323eb16a64389b95b79458"><divclass="ttname"><ahref="namespaceutil_1_1graph.html#a5c544f7c7c323eb16a64389b95b79458">util::graph::DenseIntTopologicalSortOrDie</a></div><divclass="ttdeci">std::vector< int > DenseIntTopologicalSortOrDie(int num_nodes, const std::vector< std::pair< int, int >>&arcs)</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8h_source.html#l00522">topologicalsorter.h:522</a></div></div>
<divclass="ttc"id="anamespaceutil_1_1internal_html_a64b329fcee75bf9b95b75523923b40d4"><divclass="ttname"><ahref="namespaceutil_1_1internal.html#a64b329fcee75bf9b95b75523923b40d4">util::internal::RunTopologicalSorterOrDie</a></div><divclass="ttdeci">std::vector< T > RunTopologicalSorterOrDie(Sorter *sorter, const std::vector< std::pair< T, T >>&arcs)</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8h_source.html#l00427">topologicalsorter.h:427</a></div></div>
<divclass="ttc"id="anamespaceutil_1_1internal_html_a6905ced10dd4647225a7bff423ab95f5"><divclass="ttname"><ahref="namespaceutil_1_1internal.html#a6905ced10dd4647225a7bff423ab95f5">util::internal::TopologicalSortOrDieImpl</a></div><divclass="ttdeci">std::vector< T > TopologicalSortOrDieImpl(const std::vector< T >&nodes, const std::vector< std::pair< T, T >>&arcs)</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8h_source.html#l00443">topologicalsorter.h:443</a></div></div>
<divclass="ttc"id="anamespaceutil_1_1internal_html_a84ebf3e6418a4e82ddf0dac3a0b0c488"><divclass="ttname"><ahref="namespaceutil_1_1internal.html#a84ebf3e6418a4e82ddf0dac3a0b0c488">util::internal::DenseIntTopologicalSortImpl</a></div><divclass="ttdeci">ABSL_MUST_USE_RESULT bool DenseIntTopologicalSortImpl(int num_nodes, const std::vector< std::pair< int, int >>&arcs, std::vector< int > *topological_order)</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8h_source.html#l00405">topologicalsorter.h:405</a></div></div>
<divclass="ttc"id="anamespaceutil_1_1internal_html_a98ed204bd7ff7f88ae27d73bf4ae1458"><divclass="ttname"><ahref="namespaceutil_1_1internal.html#a98ed204bd7ff7f88ae27d73bf4ae1458">util::internal::DenseIntTopologicalSortOrDieImpl</a></div><divclass="ttdeci">std::vector< int > DenseIntTopologicalSortOrDieImpl(int num_nodes, const std::vector< std::pair< int, int >>&arcs)</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8h_source.html#l00436">topologicalsorter.h:436</a></div></div>
<divclass="ttc"id="anamespaceutil_html_a10ecd35497e41c245dd91b83a6b2c63c"><divclass="ttname"><ahref="namespaceutil.html#a10ecd35497e41c245dd91b83a6b2c63c">util::TopologicalSortOrDie</a></div><divclass="ttdeci">std::vector< T > TopologicalSortOrDie(const std::vector< T >&nodes, const std::vector< std::pair< T, T >>&arcs)</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8h_source.html#l00494">topologicalsorter.h:494</a></div></div>
<divclass="ttc"id="anamespaceutil_html_a2c9089cade9c569feaf5a6a08125fd95"><divclass="ttname"><ahref="namespaceutil.html#a2c9089cade9c569feaf5a6a08125fd95">util::DenseIntTopologicalSort</a></div><divclass="ttdeci">ABSL_MUST_USE_RESULT bool DenseIntTopologicalSort(int num_nodes, const std::vector< std::pair< int, int >>&arcs, std::vector< int > *topological_order)</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8h_source.html#l00454">topologicalsorter.h:454</a></div></div>
<divclass="ttc"id="anamespaceutil_html_a49200d01f1aec6ee7837ad3e09b0d880"><divclass="ttname"><ahref="namespaceutil.html#a49200d01f1aec6ee7837ad3e09b0d880">util::TopologicalSort</a></div><divclass="ttdeci">ABSL_MUST_USE_RESULT bool TopologicalSort(const std::vector< T >&nodes, const std::vector< std::pair< T, T >>&arcs, std::vector< T > *topological_order)</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8h_source.html#l00469">topologicalsorter.h:469</a></div></div>
<divclass="ttc"id="anamespaceutil_html_a690ad981593a2fd2d70e93838a9e0caa"><divclass="ttname"><ahref="namespaceutil.html#a690ad981593a2fd2d70e93838a9e0caa">util::DenseIntStableTopologicalSortOrDie</a></div><divclass="ttdeci">std::vector< int > DenseIntStableTopologicalSortOrDie(int num_nodes, const std::vector< std::pair< int, int >>&arcs)</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8h_source.html#l00488">topologicalsorter.h:488</a></div></div>
<divclass="ttc"id="anamespaceutil_html_a9f60f83641a75dbbaaf3855541f2529e"><divclass="ttname"><ahref="namespaceutil.html#a9f60f83641a75dbbaaf3855541f2529e">util::DenseIntTopologicalSortOrDie</a></div><divclass="ttdeci">std::vector< int > DenseIntTopologicalSortOrDie(int num_nodes, const std::vector< std::pair< int, int >>&arcs)</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8h_source.html#l00483">topologicalsorter.h:483</a></div></div>
<divclass="ttc"id="anamespaceutil_html_a9f8f58bd1b46837f8305d316bb84d0e1"><divclass="ttname"><ahref="namespaceutil.html#a9f8f58bd1b46837f8305d316bb84d0e1">util::StableTopologicalSort</a></div><divclass="ttdeci">ABSL_MUST_USE_RESULT bool StableTopologicalSort(const std::vector< T >&nodes, const std::vector< std::pair< T, T >>&arcs, std::vector< T > *topological_order)</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8h_source.html#l00477">topologicalsorter.h:477</a></div></div>
<divclass="ttc"id="anamespaceutil_html_ad4cd4c6ef5dae86954f253e3911387ad"><divclass="ttname"><ahref="namespaceutil.html#ad4cd4c6ef5dae86954f253e3911387ad">util::StableTopologicalSortOrDie</a></div><divclass="ttdeci">std::vector< T > StableTopologicalSortOrDie(const std::vector< T >&nodes, const std::vector< std::pair< T, T >>&arcs)</div><divclass="ttdef"><b>Definition:</b><ahref="topologicalsorter_8h_source.html#l00500">topologicalsorter.h:500</a></div></div>