29#ifndef OR_TOOLS_GRAPH_EULERIAN_PATH_H_
30#define OR_TOOLS_GRAPH_EULERIAN_PATH_H_
39template <
typename Graph>
44template <
typename Graph>
48 if ((
graph.OutDegree(node) +
graph.InDegree(node)) % 2 != 0) {
58template <
typename NodeIndex,
typename Graph>
60 bool assume_connectivity =
true) {
61 CHECK(odd_nodes !=
nullptr);
63 const int degree =
graph.OutDegree(node) +
graph.InDegree(node);
64 if (degree % 2 != 0) {
65 odd_nodes->push_back(node);
68 if (odd_nodes->size() > 2)
return false;
77template <
typename NodeIndex,
typename Graph>
81 std::vector<bool> unvisited_edges(
graph.num_arcs(),
true);
82 std::vector<NodeIndex> tour;
83 if (
graph.IsNodeValid(root)) {
84 std::vector<NodeIndex> tour_stack = {root};
85 std::vector<ArcIndex> active_arcs(
graph.num_nodes());
87 active_arcs[node] = *(
graph.OutgoingOrOppositeIncomingArcs(node)).begin();
89 while (!tour_stack.empty()) {
91 bool has_unvisited_edges =
false;
93 graph.OutgoingOrOppositeIncomingArcsStartingFrom(
94 node, active_arcs[node])) {
96 if (unvisited_edges[
edge]) {
97 has_unvisited_edges =
true;
98 active_arcs[node] =
arc;
99 tour_stack.push_back(
graph.Head(
arc));
100 unvisited_edges[
edge] =
false;
104 if (!has_unvisited_edges) {
105 tour.push_back(node);
106 tour_stack.pop_back();
118template <
typename NodeIndex,
typename Graph>
121 std::vector<NodeIndex> tour;
130template <
typename Graph>
132 const Graph&
graph,
bool assume_connectivity =
true) {
140template <
typename Graph>
142 const Graph&
graph,
bool assume_connectivity =
true) {
144 std::vector<NodeIndex> path;
145 std::vector<NodeIndex> roots;
147 const NodeIndex root = roots.empty() ? 0 : roots.back();
154template <
typename Graph>
158 if (n <= 1)
return true;
161 std::vector<NodeIndex> stack = {0};
162 std::vector<bool> visited(n,
false);
163 while (!stack.empty()) {
166 for (
auto arc :
graph.OutgoingOrOppositeIncomingArcs(node)) {
168 if (!visited[neigh]) {
169 visited[neigh] =
true;
170 stack.push_back(neigh);
171 if (++num_visited == n)
return true;
bool GraphIsConnected(const Graph &graph)
In SWIG mode, we don't want anything besides these top-level includes.
bool IsEulerianGraph(const Graph &graph, bool assume_connectivity=true)
Returns true if a graph is Eulerian, aka all its nodes are of even degree.
std::vector< typename Graph::NodeIndex > BuildEulerianPath(const Graph &graph, bool assume_connectivity=true)
std::vector< NodeIndex > BuildEulerianTourFromNode(const Graph &graph, NodeIndex root, bool assume_connectivity=true)
util::ReverseArcStaticGraph Graph
Type of graph to use.
std::vector< typename Graph::NodeIndex > BuildEulerianTour(const Graph &graph, bool assume_connectivity=true)
bool IsSemiEulerianGraph(const Graph &graph, std::vector< NodeIndex > *odd_nodes, bool assume_connectivity=true)
std::vector< NodeIndex > BuildEulerianPathFromNode(const Graph &graph, NodeIndex root)