16 #ifndef UTIL_GRAPH_UTIL_H_ 17 #define UTIL_GRAPH_UTIL_H_ 27 #include "absl/container/flat_hash_map.h" 28 #include "absl/container/inlined_vector.h" 29 #include "ortools/base/hash.h" 30 #include "ortools/base/map_util.h" 52 template <
class Graph>
54 template <
class Graph>
56 template <
class Graph>
58 template <
class Graph>
62 template <
class Graph>
70 template <
class Graph>
72 const std::vector<int>& new_node_index);
84 template <
class Graph>
86 const std::vector<int>& nodes);
99 template <
class Graph>
105 typedef typename Graph::OutgoingOrOppositeIncomingArcIterator
ArcIterator;
112 return graph_.
Head(ArcIterator::operator*());
121 const auto& arc_range = graph_.OutgoingOrOppositeIncomingArcs(node);
133 template <
class Graph>
150 template <
class Graph>
163 template <
class Graph>
167 template <
class Graph>
185 template <
class Graph>
187 bool die_if_not_symmetric);
191 template <
class Graph>
194 if (graph.
Tail(arc) == graph.
Head(arc))
return true;
199 template <
class Graph>
203 std::vector<bool> tmp_node_mask(graph.
num_nodes(),
false);
207 if (tmp_node_mask[head])
return true;
208 tmp_node_mask[head] =
true;
211 tmp_node_mask[graph.
Head(arc)] =
false;
217 template <
class Graph>
229 reverse_graph.Build();
231 std::vector<ArcIndex> count(graph.
num_nodes(), 0);
234 ++count[graph.
Head(arc)];
236 for (
const ArcIndex arc : reverse_graph.OutgoingArcs(node)) {
237 if (--count[reverse_graph.Head(arc)] < 0)
return false;
240 if (count[graph.
Head(arc)] != 0)
return false;
246 template <
class Graph>
249 static_assert(std::numeric_limits<NodeIndex>::max() <= INT_MAX,
250 "GraphIsWeaklyConnected() isn't yet implemented for graphs" 251 " that support more than INT_MAX nodes. Reach out to" 252 " or-core-team@ if you need this.");
262 template <
class Graph>
264 std::unique_ptr<Graph> new_graph(
266 for (
const auto node : graph.
AllNodes()) {
275 template <
class Graph>
277 const std::vector<int>& new_node_index) {
279 const int num_nodes = old_graph.
num_nodes();
280 CHECK_EQ(new_node_index.size(), num_nodes);
281 std::unique_ptr<Graph> new_graph(
new Graph(num_nodes, old_graph.
num_arcs()));
286 new_graph->AddArc(new_node_index[node],
287 new_node_index[old_graph.
Head(arc)]);
294 template <
class Graph>
296 const std::vector<int>& nodes) {
300 std::vector<NodeIndex> new_node_index(old_graph.
num_nodes(), -1);
301 for (
NodeIndex new_index = 0; new_index < nodes.size(); ++new_index) {
302 new_node_index[nodes[new_index]] = new_index;
309 if (new_node_index[old_graph.
Head(arc)] != -1) ++num_arcs;
316 std::unique_ptr<Graph> new_graph(
new Graph(nodes.size(), num_arcs));
317 for (
NodeIndex new_tail = 0; new_tail < nodes.size(); ++new_tail) {
318 const NodeIndex old_tail = nodes[new_tail];
320 const NodeIndex new_head = new_node_index[old_graph.
Head(arc)];
321 if (new_head != -1) new_graph->
AddArc(new_tail, new_head);
328 template <
class Graph>
333 std::vector<bool> tmp_node_mask(graph.
num_nodes(),
false);
337 if (head != tail && !tmp_node_mask[head]) {
338 tmp_node_mask[head] =
true;
343 tmp_node_mask[graph.
Head(arc)] =
false;
350 template <
class Graph>
352 if (arc_path->empty())
return;
355 std::map<int, int> last_arc_leaving_node;
356 for (
const int arc : *arc_path) last_arc_leaving_node[graph.
Tail(arc)] = arc;
360 last_arc_leaving_node[graph.
Head(arc_path->back())] = -1;
364 int node = graph.
Tail(arc_path->front());
366 while (new_size < arc_path->size()) {
367 const int arc = gtl::FindOrDie(last_arc_leaving_node, node);
368 if (arc == -1)
break;
369 (*arc_path)[new_size++] = arc;
370 node = graph.
Head(arc);
372 arc_path->resize(new_size);
375 template <
class Graph>
377 if (arc_path.empty())
return false;
379 seen.insert(graph.
Tail(arc_path.front()));
380 for (
const int arc : arc_path) {
381 if (!gtl::InsertIfNotPresent(&seen, graph.
Head(arc)))
return true;
386 template <
class Graph>
388 const Graph& graph,
bool die_if_not_symmetric) {
389 std::vector<int> reverse_arc(graph.
num_arcs(), -1);
393 absl::flat_hash_map<std::pair< int,
int>,
394 absl::InlinedVector<int, 4>>
397 for (
int arc = 0; arc < graph.
num_arcs(); ++arc) {
398 const int tail = graph.
Tail(arc);
399 const int head = graph.
Head(arc);
402 reverse_arc[arc] = arc;
406 auto it = arc_map.find({head, tail});
407 if (it != arc_map.end()) {
410 reverse_arc[arc] = it->second.back();
411 reverse_arc[it->second.back()] = arc;
412 if (it->second.size() > 1) {
413 it->second.pop_back();
419 arc_map[{tail, head}].push_back(arc);
424 int64_t num_unmapped_arcs = 0;
425 for (
const auto& p : arc_map) {
426 num_unmapped_arcs += p.second.size();
428 DCHECK_EQ(std::count(reverse_arc.begin(), reverse_arc.end(), -1),
431 if (die_if_not_symmetric) {
432 CHECK_EQ(arc_map.size(), 0)
433 <<
"The graph is not symmetric: " << arc_map.size() <<
" of " 434 << graph.
num_arcs() <<
" arcs did not have a reverse.";
441 #endif // UTIL_GRAPH_UTIL_H_
BeginEndWrapper< OutgoingArcIterator > OutgoingArcs(NodeIndexType node) const
bool IsValidPermutation(const std::vector< int > &v)
NodeIndexType Head(ArcIndexType arc) const
std::unique_ptr< Graph > RemoveSelfArcsAndDuplicateArcs(const Graph &graph)
bool PathHasCycle(const Graph &graph, const std::vector< int > &arc_path)
AdjacencyListIterator(const Graph &graph, ArcIterator &&arc_it)
IntegerRange< ArcIndex > AllForwardArcs() const
ArcIndexType num_arcs() const
BeginEndWrapper< AdjacencyListIterator > operator[](int node) const
NodeIndexType Tail(ArcIndexType arc) const
std::vector< int > ComputeOnePossibleReverseArcMapping(const Graph &graph, bool die_if_not_symmetric)
void RemoveCyclesFromPath(const Graph &graph, std::vector< int > *arc_path)
ArcIndexType AddArc(NodeIndexType tail, NodeIndexType head)
bool GraphHasSelfArcs(const Graph &graph)
std::vector< int > GetConnectedComponents(int num_nodes, const UndirectedGraph &graph)
bool GraphIsSymmetric(const Graph &graph)
bool IsSubsetOf0N(const std::vector< int > &v, int n)
ArcIndexType AddArc(NodeIndexType tail, NodeIndexType head)
bool GraphHasDuplicateArcs(const Graph &graph)
std::unique_ptr< Graph > CopyGraph(const Graph &graph)
void SetNumberOfNodes(int num_nodes)
Graph::NodeIndex operator *() const
Graph::OutgoingOrOppositeIncomingArcIterator ArcIterator
int GetNumberOfComponents() const
IntegerRange< NodeIndex > AllNodes() const
std::vector< int > GetWeaklyConnectedComponents(const Graph &graph)
NodeIndexType num_nodes() const
std::unique_ptr< Graph > RemapGraph(const Graph &graph, const std::vector< int > &new_node_index)
bool AddEdge(int node1, int node2)
UndirectedAdjacencyListsOfDirectedGraph(const Graph &graph)
bool GraphIsWeaklyConnected(const Graph &graph)
std::unique_ptr< Graph > GetSubgraphOfNodes(const Graph &graph, const std::vector< int > &nodes)