Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
dag_connectivity.h
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef OR_TOOLS_GRAPH_DAG_CONNECTIVITY_H_
15#define OR_TOOLS_GRAPH_DAG_CONNECTIVITY_H_
16
17#include <cstdint>
18#include <utility>
19#include <vector>
20
21#include "absl/types/span.h"
22#include "ortools/util/bitset.h"
23
24namespace operations_research {
25
26// Given a directed graph, as defined by the arc list "arcs", computes either:
27// 1. If the graph is acyclic, the matrix of values x, where x[i][j] indicates
28// that there is a directed path from i to j.
29// 2. If the graph is cyclic, "error_cycle_out" is set to contain the cycle,
30// and the return value is empty.
31//
32// The algorithm runs in O(num_nodes^2 + num_nodes*num_arcs).
33//
34// Inputs:
35// arcs: each a in "arcs" is a directed edge from a.first to a.second. Must
36// have a.first, a.second >= 0. The graph is assumed to have nodes
37// {0,1,...,max_{a in arcs} max(a.first, a.second)}, or have no nodes
38// if arcs is the empty list.
39// error_was_cyclic: output arg, is set to true if a cycle is detected.
40// error_cycle_out: output arg, if a cycle is detected, error_cycle_out is
41// set to contain the nodes of the cycle in order.
42//
43// Note: useful for computing the transitive closure of a binary relation, e.g.
44// given the relation i < j for i,j in S that is transitive and some known
45// values i < j, create a node for each i in S and an arc for each known
46// relationship. Then any relationship implied by transitivity is given by
47// the resulting matrix produced, or if the relation fails transitivity, a cycle
48// proving this is produced.
49std::vector<Bitset64<int64_t>> ComputeDagConnectivity(
50 absl::Span<const std::pair<int, int>> arcs, bool* error_was_cyclic,
51 std::vector<int>* error_cycle_out);
52
53// Like above, but will CHECK fail if the digraph with arc list "arcs"
54// contains a cycle.
55std::vector<Bitset64<int64_t>> ComputeDagConnectivityOrDie(
56 absl::Span<const std::pair<int, int>> arcs);
57
58} // namespace operations_research
59
60#endif // OR_TOOLS_GRAPH_DAG_CONNECTIVITY_H_
In SWIG mode, we don't want anything besides these top-level includes.
std::vector< Bitset64< int64_t > > ComputeDagConnectivity(absl::Span< const std::pair< int, int > > arcs, bool *error_was_cyclic, std::vector< int > *error_cycle_out)
std::vector< Bitset64< int64_t > > ComputeDagConnectivityOrDie(absl::Span< const std::pair< int, int > > arcs)