<ahref="util_8h.html">Go to the documentation of this file.</a><divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> <spanclass="comment">// Copyright 2010-2018 Google LLC</span></div><divclass="line"><aname="l00002"></a><spanclass="lineno"> 2</span> <spanclass="comment">// Licensed under the Apache License, Version 2.0 (the "License");</span></div><divclass="line"><aname="l00003"></a><spanclass="lineno"> 3</span> <spanclass="comment">// you may not use this file except in compliance with the License.</span></div><divclass="line"><aname="l00004"></a><spanclass="lineno"> 4</span> <spanclass="comment">// You may obtain a copy of the License at</span></div><divclass="line"><aname="l00005"></a><spanclass="lineno"> 5</span> <spanclass="comment">//</span></div><divclass="line"><aname="l00006"></a><spanclass="lineno"> 6</span> <spanclass="comment">// http://www.apache.org/licenses/LICENSE-2.0</span></div><divclass="line"><aname="l00007"></a><spanclass="lineno"> 7</span> <spanclass="comment">//</span></div><divclass="line"><aname="l00008"></a><spanclass="lineno"> 8</span> <spanclass="comment">// Unless required by applicable law or agreed to in writing, software</span></div><divclass="line"><aname="l00009"></a><spanclass="lineno"> 9</span> <spanclass="comment">// distributed under the License is distributed on an "AS IS" BASIS,</span></div><divclass="line"><aname="l00010"></a><spanclass="lineno"> 10</span> <spanclass="comment">// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></div><divclass="line"><aname="l00011"></a><spanclass="lineno"> 11</span> <spanclass="comment">// See the License for the specific language governing permissions and</span></div><divclass="line"><aname="l00012"></a><spanclass="lineno"> 12</span> <spanclass="comment">// limitations under the License.</span></div><divclass="line"><aname="l00013"></a><spanclass="lineno"> 13</span> </div><divclass="line"><aname="l00014"></a><spanclass="lineno"> 14</span> <spanclass="comment">// A collections of utilities for the Graph classes in ./graph.h.</span></div><divclass="line"><aname="l00015"></a><spanclass="lineno"> 15</span> </div><divclass="line"><aname="l00016"></a><spanclass="lineno"> 16</span> <spanclass="preprocessor">#ifndef UTIL_GRAPH_UTIL_H_</span></div><divclass="line"><aname="l00017"></a><spanclass="lineno"> 17</span> <spanclass="preprocessor">#define UTIL_GRAPH_UTIL_H_</span></div><divclass="line"><aname="l00018"></a><spanclass="lineno"> 18</span> </div><divclass="line"><aname="l00019"></a><spanclass="lineno"> 19</span> <spanclass="preprocessor">#include <algorithm></span></div><divclass="line"><aname="l00020"></a><spanclass="lineno"> 20</span> <spanclass="preprocessor">#include <map></span></div><divclass="line"><aname="l00021"></a><spanclass="lineno"> 21</span> <spanclass="preprocessor">#include <memory></span></div><divclass="line"><aname="l00022"></a><spanclass="lineno"> 22</span> <spanclass="preprocessor">#include <set></span></div><divclass="line"><aname="l00023"></a><spanclass="lineno"> 23</span> <spanclass="preprocessor">#include <string></span></div><divclass="line"><aname="l00024"></a><spanclass="lineno"> 24</span> <spanclass="preprocessor">#include <unordered_map></span></div><divclass="line"><aname="l00025"></a><spanclass="lineno"> 25</span> <spanclass="preprocessor">#include <vector></span></div><divclass="line"><aname="l00026"></a><spanclass="lineno"> 26</span> </div><divclass="line"><aname="l00027"></a><spanclass="lineno"> 27</span> <spanclass="preprocessor">#include "ortools/base/hash.h"</span></div><divclass="line"><aname="l00028"></a><spanclass="lineno"> 28</span> <spanclass="preprocessor">
<divclass="ttc"id="namespaceutil_html_ad7986b01cf61a31c09a27b4a97db6a83"><divclass="ttname"><ahref="namespaceutil.html#ad7986b01cf61a31c09a27b4a97db6a83">util::IsValidPermutation</a></div><divclass="ttdeci">bool IsValidPermutation(const std::vector< int >&v)</div><divclass="ttdoc">Returns true iff the given vector is a permutation of [0..size()-1].</div><divclass="ttdef"><b>Definition:</b><ahref="util_8h_source.html#l00143">util.h:143</a></div></div>
<divclass="ttc"id="classutil_1_1UndirectedAdjacencyListsOfDirectedGraph_html"><divclass="ttname"><ahref="classutil_1_1UndirectedAdjacencyListsOfDirectedGraph.html">util::UndirectedAdjacencyListsOfDirectedGraph</a></div><divclass="ttdoc">This can be used to view a directed graph (that supports reverse arcs) from graph....</div><divclass="ttdef"><b>Definition:</b><ahref="util_8h_source.html#l00098">util.h:98</a></div></div>
<divclass="ttc"id="namespaceutil_html"><divclass="ttname"><ahref="namespaceutil.html">util</a></div><divclass="ttdoc">Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in c...</div><divclass="ttdef"><b>Definition:</b><ahref="connected__components_8h_source.html#l00056">connected_components.h:56</a></div></div>
<divclass="ttc"id="classDenseConnectedComponentsFinder_html_a46780e194c525fa9853fa899d69b4340"><divclass="ttname"><ahref="classDenseConnectedComponentsFinder.html#a46780e194c525fa9853fa899d69b4340">DenseConnectedComponentsFinder::SetNumberOfNodes</a></div><divclass="ttdeci">void SetNumberOfNodes(int num_nodes)</div><divclass="ttdoc">Sets the number of nodes in the graph.</div></div>
<divclass="ttc"id="namespaceutil_html_a8a06031908a024a50dbdddc394a22490"><divclass="ttname"><ahref="namespaceutil.html#a8a06031908a024a50dbdddc394a22490">util::RemoveSelfArcsAndDuplicateArcs</a></div><divclass="ttdeci">std::unique_ptr< Graph > RemoveSelfArcsAndDuplicateArcs(const Graph &graph)</div><divclass="ttdoc">Returns a copy of "graph", without self-arcs and duplicate arcs.</div><divclass="ttdef"><b>Definition:</b><ahref="util_8h_source.html#l00324">util.h:324</a></div></div>
<divclass="ttc"id="classutil_1_1UndirectedAdjacencyListsOfDirectedGraph_1_1AdjacencyListIterator_html_a9c411230b6cd51ca80cb31a1f8387ae8"><divclass="ttname"><ahref="classutil_1_1UndirectedAdjacencyListsOfDirectedGraph_1_1AdjacencyListIterator.html#a9c411230b6cd51ca80cb31a1f8387ae8">util::UndirectedAdjacencyListsOfDirectedGraph::AdjacencyListIterator::operator *</a></div><divclass="ttdeci">Graph::NodeIndex operator *() const</div><divclass="ttdoc">Overwrite operator* to return the heads of the arcs.</div><divclass="ttdef"><b>Definition:</b><ahref="util_8h_source.html#l00109">util.h:109</a></div></div>
<divclass="ttc"id="namespaceutil_html_ae76339cb2dcd3bc05ad762146f91fdda"><divclass="ttname"><ahref="namespaceutil.html#ae76339cb2dcd3bc05ad762146f91fdda">util::Graph</a></div><divclass="ttdeci">ListGraph Graph</div><divclass="ttdoc">Defining the simplest Graph interface as Graph for convenience.</div><divclass="ttdef"><b>Definition:</b><ahref="graph_8h_source.html#l02358">graph.h:2358</a></div></div>
<divclass="ttc"id="namespaceutil_html_ae469c559688b92f36bae2788c2e6063e"><divclass="ttname"><ahref="namespaceutil.html#ae469c559688b92f36bae2788c2e6063e">util::ComputeOnePossibleReverseArcMapping</a></div><divclass="ttdeci">std::vector< int > ComputeOnePossibleReverseArcMapping(const Graph &graph, bool die_if_not_symmetric)</div><divclass="ttdoc">Returns a vector representing a mapping from arcs to arcs such that each arc is mapped to another arc...</div><divclass="ttdef"><b>Definition:</b><ahref="util_8h_source.html#l00382">util.h:382</a></div></div>
<divclass="ttc"id="classutil_1_1BaseGraph_html_a0f551c921fa0b5aaa334a6e36f61db4a"><divclass="ttname"><ahref="classutil_1_1BaseGraph.html#a0f551c921fa0b5aaa334a6e36f61db4a">util::BaseGraph::num_nodes</a></div><divclass="ttdeci">NodeIndexType num_nodes() const</div><divclass="ttdoc">Returns the number of valid nodes in the graph.</div><divclass="ttdef"><b>Definition:</b><ahref="graph_8h_source.html#l00201">graph.h:201</a></div></div>
<divclass="ttc"id="classDenseConnectedComponentsFinder_html"><divclass="ttname"><ahref="classDenseConnectedComponentsFinder.html">DenseConnectedComponentsFinder</a></div><divclass="ttdoc">NOTE(user): The rest of the functions below should also be in namespace util, but for historical reas...</div><divclass="ttdef"><b>Definition:</b><ahref="connected__components_8h_source.html#l00081">connected_components.h:81</a></div></div>
<divclass="ttc"id="classutil_1_1ListGraph_html_aba03f198f690155bca2ab23b039cea54"><divclass="ttname"><ahref="classutil_1_1ListGraph.html#aba03f198f690155bca2ab23b039cea54">util::ListGraph::Tail</a></div><divclass="ttdeci">NodeIndexType Tail(ArcIndexType arc) const</div><divclass="ttdoc">Returns the tail/head of a valid arc.</div><divclass="ttdef"><b>Definition:</b><ahref="graph_8h_source.html#l01107">graph.h:1107</a></div></div>
<divclass="ttc"id="namespaceutil_html_a13f0e8f7e15873600cf8e395958c71e1"><divclass="ttname"><ahref="namespaceutil.html#a13f0e8f7e15873600cf8e395958c71e1">util::GetConnectedComponents</a></div><divclass="ttdeci">std::vector< int > GetConnectedComponents(int num_nodes, const UndirectedGraph &graph)</div><divclass="ttdoc">Finds the connected components of the graph, using BFS internally.</div><divclass="ttdef"><b>Definition:</b><ahref="connected__components_8h_source.html#l00289">connected_components.h:289</a></div></div>
<divclass="ttc"id="namespaceutil_html_ac4af76993c891ee4ad507783edec2a1c"><divclass="ttname"><ahref="namespaceutil.html#ac4af76993c891ee4ad507783edec2a1c">util::GraphHasSelfArcs</a></div><divclass="ttdeci">bool GraphHasSelfArcs(const Graph &graph)</div><divclass="ttdoc">Here's a set of simple diagnosis tools.</div><divclass="ttdef"><b>Definition:</b><ahref="util_8h_source.html#l00187">util.h:187</a></div></div>
<divclass="ttc"id="namespaceutil_html_acfecdce43e9933bde2a94fd879f12f5f"><divclass="ttname"><ahref="namespaceutil.html#acfecdce43e9933bde2a94fd879f12f5f">util::RemapGraph</a></div><divclass="ttdeci">std::unique_ptr< Graph > RemapGraph(const Graph &graph, const std::vector< int >&new_node_index)</div><divclass="ttdoc">Creates a remapped copy of graph "graph", where node i becomes node new_node_index[i].</div><divclass="ttdef"><b>Definition:</b><ahref="util_8h_source.html#l00271">util.h:271</a></div></div>
<divclass="ttc"id="namespaceutil_html_aa9fb4c9a176acaf72053b11727436e9e"><divclass="ttname"><ahref="namespaceutil.html#aa9fb4c9a176acaf72053b11727436e9e">util::IsSubsetOf0N</a></div><divclass="ttdeci">bool IsSubsetOf0N(const std::vector< int >&v, int n)</div><divclass="ttdoc">Returns true iff the given vector is a subset of [0..n-1], i.e.</div></div>
<divclass="ttc"id="namespaceutil_html_ad1df170a504d335462a1104a942e6069"><divclass="ttname"><ahref="namespaceutil.html#ad1df170a504d335462a1104a942e6069">util::GetSubgraphOfNodes</a></div><divclass="ttdeci">std::unique_ptr< Graph > GetSubgraphOfNodes(const Graph &graph, const std::vector< int >&nodes)</div><divclass="ttdoc">Gets the induced subgraph of "graph" restricted to the nodes in "nodes": the resulting graph will hav...</div><divclass="ttdef"><b>Definition:</b><ahref="util_8h_source.html#l00290">util.h:290</a></div></div>
<divclass="ttc"id="classutil_1_1StaticGraph_html"><divclass="ttname"><ahref="classutil_1_1StaticGraph.html">util::StaticGraph</a></div><divclass="ttdoc">Most efficient implementation of a graph without reverse arcs:</div><divclass="ttdef"><b>Definition:</b><ahref="graph_8h_source.html#l00396">graph.h:396</a></div></div>
<divclass="ttc"id="classutil_1_1ListGraph_html_ab63770fa993b9347586d2852b841bdc2"><divclass="ttname"><ahref="classutil_1_1ListGraph.html#ab63770fa993b9347586d2852b841bdc2">util::ListGraph::OutgoingArcs</a></div><divclass="ttdeci">BeginEndWrapper< OutgoingArcIterator > OutgoingArcs(NodeIndexType node) const</div><divclass="ttdoc">Allows to iterate over the forward arcs that verify Tail(arc) == node.</div></div>
<divclass="ttc"id="namespaceutil_html_adbb18bcb2f9d64cbbaeb57c328f57e7b"><divclass="ttname"><ahref="namespaceutil.html#adbb18bcb2f9d64cbbaeb57c328f57e7b">util::PathHasCycle</a></div><divclass="ttdeci">bool PathHasCycle(const Graph &graph, const std::vector< int >&arc_path)</div><divclass="ttdoc">Returns true iff the given path contains a cycle.</div><divclass="ttdef"><b>Definition:</b><ahref="util_8h_source.html#l00371">util.h:371</a></div></div>
<divclass="ttc"id="classutil_1_1BaseGraph_html_a32ba4a5ca9a4b89f750eb2dc56518b02"><divclass="ttname"><ahref="classutil_1_1BaseGraph.html#a32ba4a5ca9a4b89f750eb2dc56518b02">util::BaseGraph::AllNodes</a></div><divclass="ttdeci">IntegerRange< NodeIndex > AllNodes() const</div><divclass="ttdoc">Allows nice range-based for loop: for (const NodeIndex node : graph.AllNodes()) { ....</div><divclass="ttdef"><b>Definition:</b><ahref="graph_8h_source.html#l00929">graph.h:929</a></div></div>
<divclass="ttc"id="classutil_1_1BaseGraph_html_aefb468e4d4a3128c91b3bad9f5b314c8"><divclass="ttname"><ahref="classutil_1_1BaseGraph.html#aefb468e4d4a3128c91b3bad9f5b314c8">util::BaseGraph::num_arcs</a></div><divclass="ttdeci">ArcIndexType num_arcs() const</div><divclass="ttdoc">Returns the number of valid arcs in the graph.</div><divclass="ttdef"><b>Definition:</b><ahref="graph_8h_source.html#l00204">graph.h:204</a></div></div>
<divclass="ttc"id="namespaceutil_html_ab34783e729bb5fc99042893f6bfcbb2f"><divclass="ttname"><ahref="namespaceutil.html#ab34783e729bb5fc99042893f6bfcbb2f">util::GetWeaklyConnectedComponents</a></div><divclass="ttdeci">std::vector< int > GetWeaklyConnectedComponents(const Graph &graph)</div><divclass="ttdoc">Computes the weakly connected components of a directed graph that provides the OutgoingOrOppositeInco...</div><divclass="ttdef"><b>Definition:</b><ahref="util_8h_source.html#l00132">util.h:132</a></div></div>
<divclass="ttc"id="classutil_1_1BaseGraph_html_adb271cf4bcf2de5b5bbe300d7054af29"><divclass="ttname"><ahref="classutil_1_1BaseGraph.html#adb271cf4bcf2de5b5bbe300d7054af29">util::BaseGraph< NodeIndexType, ArcIndexType, false >::NodeIndex</a></div><divclass="ttdeci">NodeIndexType NodeIndex</div><divclass="ttdoc">Typedef so you can use Graph::NodeIndex and Graph::ArcIndex to be generic but also to improve the rea...</div><divclass="ttdef"><b>Definition:</b><ahref="graph_8h_source.html#l00189">graph.h:189</a></div></div>
<divclass="ttc"id="classDenseConnectedComponentsFinder_html_aba450cf3f16d9386d043c4354a19a4aa"><divclass="ttname"><ahref="classDenseConnectedComponentsFinder.html#aba450cf3f16d9386d043c4354a19a4aa">DenseConnectedComponentsFinder::AddEdge</a></div><divclass="ttdeci">void AddEdge(int node1, int node2)</div><divclass="ttdoc">The main API is the same as ConnectedComponentsFinder (below): see the homonymous functions there.</div></div>
<divclass="ttc"id="namespaceutil_html_a06fa201576c927d92657e090fa86bfdb"><divclass="ttname"><ahref="namespaceutil.html#a06fa201576c927d92657e090fa86bfdb">util::RemoveCyclesFromPath</a></div><divclass="ttdeci">void RemoveCyclesFromPath(const Graph &graph, std::vector< int > *arc_path)</div><divclass="ttdoc">Given an arc path, changes it to a sub-path with the same source and destination but without any cycl...</div><divclass="ttdef"><b>Definition:</b><ahref="util_8h_source.html#l00346">util.h:346</a></div></div>
<divclass="ttc"id="namespaceutil_html_a0ed748741b17dad9e6cc485728bb0043"><divclass="ttname"><ahref="namespaceutil.html#a0ed748741b17dad9e6cc485728bb0043">util::CopyGraph</a></div><divclass="ttdeci">std::unique_ptr< Graph > CopyGraph(const Graph &graph)</div><divclass="ttdoc">Returns a fresh copy of a given graph.</div><divclass="ttdef"><b>Definition:</b><ahref="util_8h_source.html#l00258">util.h:258</a></div></div>
<divclass="ttc"id="namespaceoperations__research_html_a0e629e35bfa311b31dd7f5065eb834bb"><divclass="ttname"><ahref="namespaceoperations__research.html#a0e629e35bfa311b31dd7f5065eb834bb">operations_research::NodeIndex</a></div><divclass="ttdeci">int32 NodeIndex</div><divclass="ttdoc">Standard instantiation of ForwardEbertGraph (named 'ForwardStarGraph') of EbertGraph (named 'StarGrap...</div><divclass="ttdef"><b>Definition:</b><ahref="ebert__graph_8h_source.html#l00192">ebert_graph.h:192</a></div></div>
<divclass="ttc"id="classutil_1_1ListGraph_html"><divclass="ttname"><ahref="classutil_1_1ListGraph.html">util::ListGraph</a></div><divclass="ttdoc">Basic graph implementation without reverse arc.</div><divclass="ttdef"><b>Definition:</b><ahref="graph_8h_source.html#l00297">graph.h:297</a></div></div>
<divclass="ttc"id="classutil_1_1UndirectedAdjacencyListsOfDirectedGraph_html_a8d68da65b93ce8e85c66d431858897bb"><divclass="ttname"><ahref="classutil_1_1UndirectedAdjacencyListsOfDirectedGraph.html#a8d68da65b93ce8e85c66d431858897bb">util::UndirectedAdjacencyListsOfDirectedGraph::operator[]</a></div><divclass="ttdeci">BeginEndWrapper< AdjacencyListIterator > operator[](int node) const</div><divclass="ttdoc">Returns a pseudo-container of all the nodes adjacent to "node".</div><divclass="ttdef"><b>Definition:</b><ahref="util_8h_source.html#l00118">util.h:118</a></div></div>