<ahref="connected__components_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="l00015"name="l00015"></a><spanclass="lineno"> 15</span><spanclass="comment">// Licensed under the Apache License, Version 2.0 (the "License");</span></div>
<divclass="line"><aid="l00016"name="l00016"></a><spanclass="lineno"> 16</span><spanclass="comment">// you may not use this file except in compliance with the License.</span></div>
<divclass="line"><aid="l00017"name="l00017"></a><spanclass="lineno"> 17</span><spanclass="comment">// You may obtain a copy of the License at</span></div>
<divclass="line"><aid="l00021"name="l00021"></a><spanclass="lineno"> 21</span><spanclass="comment">// Unless required by applicable law or agreed to in writing, software</span></div>
<divclass="line"><aid="l00022"name="l00022"></a><spanclass="lineno"> 22</span><spanclass="comment">// distributed under the License is distributed on an "AS IS" BASIS,</span></div>
<divclass="line"><aid="l00023"name="l00023"></a><spanclass="lineno"> 23</span><spanclass="comment">// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></div>
<divclass="line"><aid="l00024"name="l00024"></a><spanclass="lineno"> 24</span><spanclass="comment">// See the License for the specific language governing permissions and</span></div>
<divclass="line"><aid="l00025"name="l00025"></a><spanclass="lineno"> 25</span><spanclass="comment">// limitations under the License.</span></div>
<divclass="line"><aid="l00027"name="l00027"></a><spanclass="lineno"> 27</span><spanclass="comment">// Finds the connected components in an undirected graph:</span></div>
<divclass="line"><aid="l00030"name="l00030"></a><spanclass="lineno"> 30</span><spanclass="comment">// If you have a fixed graph where the node are dense integers, use</span></div>
<divclass="line"><aid="l00031"name="l00031"></a><spanclass="lineno"> 31</span><spanclass="comment">// GetConnectedComponents(): it's very fast and uses little memory.</span></div>
<divclass="line"><aid="l00033"name="l00033"></a><spanclass="lineno"> 33</span><spanclass="comment">// If you have a more dynamic scenario where you want to incrementally</span></div>
<divclass="line"><aid="l00034"name="l00034"></a><spanclass="lineno"> 34</span><spanclass="comment">// add nodes or edges and query the connectivity between them, use the</span></div>
<divclass="line"><aid="l00035"name="l00035"></a><spanclass="lineno"> 35</span><spanclass="comment">// [Dense]ConnectedComponentsFinder class, which uses the union-find algorithm</span></div>
<divclass="line"><aid="l00057"name="l00057"></a><spanclass="lineno"> 57</span><spanclass="comment">// Finds the connected components of the graph, using BFS internally.</span></div>
<divclass="line"><aid="l00058"name="l00058"></a><spanclass="lineno"> 58</span><spanclass="comment">// Works on any *undirected* graph class whose nodes are dense integers and that</span></div>
<divclass="line"><aid="l00059"name="l00059"></a><spanclass="lineno"> 59</span><spanclass="comment">// supports the [] operator for adjacency lists: graph[x] must be an integer</span></div>
<divclass="line"><aid="l00060"name="l00060"></a><spanclass="lineno"> 60</span><spanclass="comment">// container listing the nodes that are adjacent to node #x.</span></div>
<divclass="line"><aid="l00063"name="l00063"></a><spanclass="lineno"> 63</span><spanclass="comment">// "Undirected" means that for all y in graph[x], x is in graph[y].</span></div>
<divclass="line"><aid="l00065"name="l00065"></a><spanclass="lineno"> 65</span><spanclass="comment">// Returns the mapping from node to component index. The component indices are</span></div>
<divclass="line"><aid="l00066"name="l00066"></a><spanclass="lineno"> 66</span><spanclass="comment">// deterministic: Component #0 will be the one that has node #0, component #1</span></div>
<divclass="line"><aid="l00067"name="l00067"></a><spanclass="lineno"> 67</span><spanclass="comment">// the one that has the lowest-index node that isn't in component #0, and so on.</span></div>
<divclass="line"><aid="l00069"name="l00069"></a><spanclass="lineno"> 69</span><spanclass="comment">// Example on the following 6-node graph: 5--3--0--1 2--4</span></div>
<divclass="line"><aid="l00077"name="l00077"></a><spanclass="lineno"> 77</span><spanclass="comment">// NOTE(user): The rest of the functions below should also be in namespace</span></div>
<divclass="line"><aid="l00078"name="l00078"></a><spanclass="lineno"> 78</span><spanclass="comment">// util, but for historical reasons it hasn't been done yet.</span></div>
<divclass="line"><aid="l00080"name="l00080"></a><spanclass="lineno"> 80</span><spanclass="comment">// A connected components finder that only works on dense ints.</span></div>
<divclass="line"><aid="l00085"name="l00085"></a><spanclass="lineno"> 85</span><spanclass="comment">// We support copy and move construction.</span></div>
<divclass="line"><aid="l00094"name="l00094"></a><spanclass="lineno"> 94</span><spanclass="comment">// The main API is the same as ConnectedComponentsFinder (below): see the</span></div>
<divclass="line"><aid="l00102"name="l00102"></a><spanclass="lineno"> 102</span><spanclass="comment">// Gets the current set of root nodes in sorted order. Runs in amortized</span></div>
<divclass="line"><aid="l00106"name="l00106"></a><spanclass="lineno"> 106</span><spanclass="comment">// Sets the number of nodes in the graph. The graph can only grow: this</span></div>
<divclass="line"><aid="l00107"name="l00107"></a><spanclass="lineno"> 107</span><spanclass="comment">// dies if "num_nodes" is lower or equal to any of the values ever given</span></div>
<divclass="line"><aid="l00108"name="l00108"></a><spanclass="lineno"> 108</span><spanclass="comment">// to AddEdge(), or lower than a previous value given to SetNumberOfNodes().</span></div>
<divclass="line"><aid="l00109"name="l00109"></a><spanclass="lineno"> 109</span><spanclass="comment">// You need this if there are nodes that don't have any edges.</span></div>
<divclass="line"><aid="l00112"name="l00112"></a><spanclass="lineno"> 112</span><spanclass="comment">// Returns the root of the set for the given node. node must be in</span></div>
<divclass="line"><aid="l00114"name="l00114"></a><spanclass="lineno"> 114</span><spanclass="comment">// Non-const because it does path compression internally.</span></div>
<divclass="line"><aid="l00117"name="l00117"></a><spanclass="lineno"> 117</span><spanclass="comment">// Returns the same as GetConnectedComponents().</span></div>
<divclass="line"><aid="l00121"name="l00121"></a><spanclass="lineno"> 121</span><spanclass="comment">// parent[i] is the id of an ancestor for node i. A node is a root iff</span></div>
<divclass="line"><aid="l00124"name="l00124"></a><spanclass="lineno"> 124</span><spanclass="comment">// If i is a root, component_size_[i] is the number of elements in the</span></div>
<divclass="line"><aid="l00125"name="l00125"></a><spanclass="lineno"> 125</span><spanclass="comment">// component. If i is not a root, component_size_[i] is meaningless.</span></div>
<divclass="line"><aid="l00131"name="l00131"></a><spanclass="lineno"> 131</span><spanclass="comment">// The current roots. This is maintained lazily by GetComponentRoots().</span></div>
<divclass="line"><aid="l00133"name="l00133"></a><spanclass="lineno"> 133</span><spanclass="comment">// The number of nodes that existed the last time GetComponentRoots() was</span></div>
<divclass="line"><aid="l00139"name="l00139"></a><spanclass="lineno"> 139</span><spanclass="comment">// A helper to deduce the type of map to use depending on whether CompareOrHashT</span></div>
<divclass="line"><aid="l00140"name="l00140"></a><spanclass="lineno"> 140</span><spanclass="comment">// is a comparator or a hasher (prefer the latter).</span></div>
<divclass="line"><aid="l00143"name="l00143"></a><spanclass="lineno"> 143</span><spanclass="comment">// SFINAE trait to detect hash functors and select unordered containers if so,</span></div>
<divclass="line"><aid="l00144"name="l00144"></a><spanclass="lineno"> 144</span><spanclass="comment">// and ordered containers otherwise (= by default).</span></div>
<divclass="line"><aid="l00145"name="l00145"></a><spanclass="lineno"> 145</span><spanclass="keyword">template</span><<spanclass="keyword">typename</span> U, <spanclass="keyword">typename</span> V, <spanclass="keyword">typename</span> E = <spanclass="keywordtype">void</span>></div>
<divclass="line"><aid="l00151"name="l00151"></a><spanclass="lineno"> 151</span><spanclass="comment">// Specialization for when U is a hash functor and Eq is void (no custom</span></div>
<divclass="line"><aid="l00153"name="l00153"></a><spanclass="lineno"> 153</span><spanclass="comment">// The expression inside decltype is basically saying that "H(x)" is</span></div>
<divclass="line"><aid="l00154"name="l00154"></a><spanclass="lineno"> 154</span><spanclass="comment">// well-formed, where H is an instance of U and x is an instance of T, and is</span></div>
<divclass="line"><aid="l00155"name="l00155"></a><spanclass="lineno"> 155</span><spanclass="comment">// a value of integral type. That is, we are "duck-typing" on whether U looks</span></div>
<divclass="line"><aid="l00156"name="l00156"></a><spanclass="lineno"> 156</span><spanclass="comment">// like a hash functor.</span></div>
<divclass="line"><aid="l00157"name="l00157"></a><spanclass="lineno"> 157</span><spanclass="keyword">template</span><<spanclass="keyword">typename</span> U, <spanclass="keyword">typename</span> V></div>
<divclass="line"><aid="l00167"name="l00167"></a><spanclass="lineno"> 167</span><spanclass="comment">// Specialization for when U is a hash functor and Eq is provided (not void).</span></div>
<divclass="line"><aid="l00168"name="l00168"></a><spanclass="lineno"> 168</span><spanclass="keyword">template</span><<spanclass="keyword">typename</span> U, <spanclass="keyword">typename</span> V></div>
<divclass="line"><aid="l00189"name="l00189"></a><spanclass="lineno"> 189</span><spanclass="comment">// ... repeating, adding nodes and edges as needed. Adding an edge</span></div>
<divclass="line"><aid="l00190"name="l00190"></a><spanclass="lineno"> 190</span><spanclass="comment">// will automatically also add the two nodes at its ends, if they</span></div>
<divclass="line"><aid="l00191"name="l00191"></a><spanclass="lineno"> 191</span><spanclass="comment">// haven't already been added.</span></div>
<divclass="line"><aid="l00194"name="l00194"></a><spanclass="lineno"> 194</span><spanclass="comment">// Each entry in components now contains all the nodes in a single</span></div>
<divclass="line"><aid="l00197"name="l00197"></a><spanclass="lineno"> 197</span><spanclass="comment">// Protocol buffers can be used as the node type. Equality and hash functions</span></div>
<divclass="line"><aid="l00198"name="l00198"></a><spanclass="lineno"> 198</span><spanclass="comment">// for protocol buffers can be found in ortools/base/message_hasher.h.</span></div>
<divclass="line"><aid="l00210"name="l00210"></a><spanclass="lineno"> 210</span><spanclass="comment">// If you want to, you can continue adding nodes and edges after calling</span></div>
<divclass="line"><aid="l00211"name="l00211"></a><spanclass="lineno"> 211</span><spanclass="comment">// FindConnectedComponents, then call it again later.</span></div>
<divclass="line"><aid="l00213"name="l00213"></a><spanclass="lineno"> 213</span><spanclass="comment">// If your node type isn't STL-friendly, then you can use pointers to</span></div>
<divclass="line"><aid="l00214"name="l00214"></a><spanclass="lineno"> 214</span><spanclass="comment">// it instead:</span></div>
<divclass="line"><aid="l00217"name="l00217"></a><spanclass="lineno"> 217</span><spanclass="comment">// ... and so on...</span></div>
<divclass="line"><aid="l00218"name="l00218"></a><spanclass="lineno"> 218</span><spanclass="comment">// Of course, in this usage, the connected components finder retains</span></div>
<divclass="line"><aid="l00219"name="l00219"></a><spanclass="lineno"> 219</span><spanclass="comment">// these pointers through its lifetime (though it doesn't dereference them).</span></div>
<divclass="line"><aid="l00235"name="l00235"></a><spanclass="lineno"> 235</span><spanclass="comment">// Adds a node in the graph. It is OK to add the same node more than</span></div>
<divclass="line"><aid="l00236"name="l00236"></a><spanclass="lineno"> 236</span><spanclass="comment">// once; additions after the first have no effect.</span></div>
<divclass="line"><aid="l00239"name="l00239"></a><spanclass="lineno"> 239</span><spanclass="comment">// Adds an edge in the graph. Also adds both endpoint nodes as necessary.</span></div>
<divclass="line"><aid="l00240"name="l00240"></a><spanclass="lineno"> 240</span><spanclass="comment">// It is not an error to add the same edge twice. Self-edges are OK too.</span></div>
<divclass="line"><aid="l00241"name="l00241"></a><spanclass="lineno"> 241</span><spanclass="comment">// Returns true if the two nodes are newly connected, and false if they were</span></div>
<divclass="line"><aid="l00248"name="l00248"></a><spanclass="lineno"> 248</span><spanclass="comment">// Returns true iff both nodes are in the same connected component.</span></div>
<divclass="line"><aid="l00249"name="l00249"></a><spanclass="lineno"> 249</span><spanclass="comment">// Returns false if either node has not been already added with AddNode.</span></div>
<divclass="line"><aid="l00250"name="l00250"></a><spanclass="lineno"><aclass="line"href="class_connected_components_finder.html#ac7782f36c09257f370347166e02480f1"> 250</a></span><spanclass="keywordtype">bool</span><aclass="code hl_function"href="class_connected_components_finder.html#ac7782f36c09257f370347166e02480f1">Connected</a>(T node1, T node2) {</div>
<divclass="line"><aid="l00255"name="l00255"></a><spanclass="lineno"> 255</span><spanclass="comment">// Finds the connected component containing a node, and returns the</span></div>
<divclass="line"><aid="l00256"name="l00256"></a><spanclass="lineno"> 256</span><spanclass="comment">// total number of nodes in that component. Returns zero iff the</span></div>
<divclass="line"><aid="l00257"name="l00257"></a><spanclass="lineno"> 257</span><spanclass="comment">// node has not been already added with AddNode.</span></div>
<divclass="line"><aid="l00262"name="l00262"></a><spanclass="lineno"> 262</span><spanclass="comment">// Finds all the connected components and assigns them to components.</span></div>
<divclass="line"><aid="l00263"name="l00263"></a><spanclass="lineno"> 263</span><spanclass="comment">// Components are ordered in the same way nodes were added, i.e. if node 'b'</span></div>
<divclass="line"><aid="l00264"name="l00264"></a><spanclass="lineno"> 264</span><spanclass="comment">// was added before node 'c', then either:</span></div>
<divclass="line"><aid="l00265"name="l00265"></a><spanclass="lineno"> 265</span><spanclass="comment">// - 'c' belongs to the same component as a node 'a' added before 'b', or</span></div>
<divclass="line"><aid="l00266"name="l00266"></a><spanclass="lineno"> 266</span><spanclass="comment">// - the component for 'c' comes after the one for 'b'.</span></div>
<divclass="line"><aid="l00267"name="l00267"></a><spanclass="lineno"> 267</span><spanclass="comment">// There are two versions:</span></div>
<divclass="line"><aid="l00268"name="l00268"></a><spanclass="lineno"> 268</span><spanclass="comment">// - The first one returns the result, and stores each component in a vector.</span></div>
<divclass="line"><aid="l00269"name="l00269"></a><spanclass="lineno"> 269</span><spanclass="comment">// This is the preferred version.</span></div>
<divclass="line"><aid="l00270"name="l00270"></a><spanclass="lineno"> 270</span><spanclass="comment">// - The second one populates the result, and stores each component in a set.</span></div>
<divclass="line"><aid="l00288"name="l00288"></a><spanclass="lineno"> 288</span><spanclass="comment">// Returns the current number of connected components.</span></div>
<divclass="line"><aid="l00289"name="l00289"></a><spanclass="lineno"> 289</span><spanclass="comment">// This number can change as the new nodes or edges are added.</span></div>
<divclass="line"><aid="l00294"name="l00294"></a><spanclass="lineno"> 294</span><spanclass="comment">// Returns the current number of added distinct nodes.</span></div>
<divclass="line"><aid="l00295"name="l00295"></a><spanclass="lineno"> 295</span><spanclass="comment">// This includes nodes added explicitly via the calls to AddNode() method</span></div>
<divclass="line"><aid="l00296"name="l00296"></a><spanclass="lineno"> 296</span><spanclass="comment">// and implicitly via the calls to AddEdge() method.</span></div>
<divclass="line"><aid="l00297"name="l00297"></a><spanclass="lineno"> 297</span><spanclass="comment">// Nodes that were added several times only count once.</span></div>
<divclass="line"><aid="l00301"name="l00301"></a><spanclass="lineno"> 301</span><spanclass="comment">// Returns the index for the given node. If the node does not exist and</span></div>
<divclass="line"><aid="l00302"name="l00302"></a><spanclass="lineno"> 302</span><spanclass="comment">// update_delegate is true, explicitly add the node to the delegate.</span></div>
<divclass="line"><aid="l00320"name="l00320"></a><spanclass="lineno"> 320</span><spanclass="comment">// Implementations of the method templates</span></div>
<divclass="ttc"id="aclass_connected_components_finder_html_a4361d2493d16cf506091e88190604ebf"><divclass="ttname"><ahref="class_connected_components_finder.html#a4361d2493d16cf506091e88190604ebf">ConnectedComponentsFinder::FindConnectedComponents</a></div><divclass="ttdeci">std::vector< std::vector< T >> FindConnectedComponents()</div><divclass="ttdef"><b>Definition:</b><ahref="connected__components_8h_source.html#l00271">connected_components.h:271</a></div></div>
<divclass="ttc"id="aclass_connected_components_finder_html_a52a1af0d0ad4b70e83987131b8585cab"><divclass="ttname"><ahref="class_connected_components_finder.html#a52a1af0d0ad4b70e83987131b8585cab">ConnectedComponentsFinder::AddEdge</a></div><divclass="ttdeci">bool AddEdge(T node1, T node2)</div><divclass="ttdef"><b>Definition:</b><ahref="connected__components_8h_source.html#l00243">connected_components.h:243</a></div></div>
<divclass="ttc"id="aclass_connected_components_finder_html_aacab55f2e1323ac0400649902f660f18"><divclass="ttname"><ahref="class_connected_components_finder.html#aacab55f2e1323ac0400649902f660f18">ConnectedComponentsFinder::operator=</a></div><divclass="ttdeci">ConnectedComponentsFinder & operator=(const ConnectedComponentsFinder &)=delete</div></div>
<divclass="ttc"id="aclass_connected_components_finder_html_ac7782f36c09257f370347166e02480f1"><divclass="ttname"><ahref="class_connected_components_finder.html#ac7782f36c09257f370347166e02480f1">ConnectedComponentsFinder::Connected</a></div><divclass="ttdeci">bool Connected(T node1, T node2)</div><divclass="ttdef"><b>Definition:</b><ahref="connected__components_8h_source.html#l00250">connected_components.h:250</a></div></div>
<divclass="ttc"id="aclass_connected_components_finder_html_afda6ee20c3c31153322022a67a31ee00"><divclass="ttname"><ahref="class_connected_components_finder.html#afda6ee20c3c31153322022a67a31ee00">ConnectedComponentsFinder::FindConnectedComponents</a></div><divclass="ttdeci">void FindConnectedComponents(std::vector< Set > *components)</div><divclass="ttdef"><b>Definition:</b><ahref="connected__components_8h_source.html#l00279">connected_components.h:279</a></div></div>
<divclass="ttc"id="aclass_dense_connected_components_finder_html_a428ab6b7c944afe33bd86a6a1ae7e668"><divclass="ttname"><ahref="class_dense_connected_components_finder.html#a428ab6b7c944afe33bd86a6a1ae7e668">DenseConnectedComponentsFinder::AddEdge</a></div><divclass="ttdeci">bool AddEdge(int node1, int node2)</div><divclass="ttdef"><b>Definition:</b><ahref="connected__components_8cc_source.html#l00096">connected_components.cc:96</a></div></div>
<divclass="ttc"id="aclass_dense_connected_components_finder_html_a71e9b6f8626a94d86b8e6fbd60fde74d"><divclass="ttname"><ahref="class_dense_connected_components_finder.html#a71e9b6f8626a94d86b8e6fbd60fde74d">DenseConnectedComponentsFinder::operator=</a></div><divclass="ttdeci">DenseConnectedComponentsFinder & operator=(DenseConnectedComponentsFinder &&)=default</div></div>
<divclass="ttc"id="aclass_dense_connected_components_finder_html_a74aadb96adc6f37110393d5fbd2279c5"><divclass="ttname"><ahref="class_dense_connected_components_finder.html#a74aadb96adc6f37110393d5fbd2279c5">DenseConnectedComponentsFinder::GetComponentIds</a></div><divclass="ttdeci">std::vector< int > GetComponentIds()</div><divclass="ttdef"><b>Definition:</b><ahref="connected__components_8cc_source.html#l00149">connected_components.cc:149</a></div></div>
<divclass="ttc"id="aclass_dense_connected_components_finder_html_a962b54327591b21cc0a9273f78906a8b"><divclass="ttname"><ahref="class_dense_connected_components_finder.html#a962b54327591b21cc0a9273f78906a8b">DenseConnectedComponentsFinder::Connected</a></div><divclass="ttdeci">bool Connected(int node1, int node2)</div><divclass="ttdef"><b>Definition:</b><ahref="connected__components_8cc_source.html#l00134">connected_components.cc:134</a></div></div>
<divclass="ttc"id="aclass_dense_connected_components_finder_html_ab10946179c4a27ddbe7be98946cb2706"><divclass="ttname"><ahref="class_dense_connected_components_finder.html#ab10946179c4a27ddbe7be98946cb2706">DenseConnectedComponentsFinder::operator=</a></div><divclass="ttdeci">DenseConnectedComponentsFinder & operator=(const DenseConnectedComponentsFinder &)=default</div></div>
<divclass="ttc"id="aclass_dense_connected_components_finder_html_ae6201f6f09303eabd7f77e238b4f3a44"><divclass="ttname"><ahref="class_dense_connected_components_finder.html#ae6201f6f09303eabd7f77e238b4f3a44">DenseConnectedComponentsFinder::GetComponentRoots</a></div><divclass="ttdeci">const std::vector< int >& GetComponentRoots()</div><divclass="ttdef"><b>Definition:</b><ahref="connected__components_8cc_source.html#l00073">connected_components.cc:73</a></div></div>