29#ifndef ORTOOLS_GRAPH_EULERIAN_PATH_H_
30#define ORTOOLS_GRAPH_EULERIAN_PATH_H_
40template <
typename Graph>
45template <
typename Graph>
59template <
typename NodeIndex,
typename Graph>
61 bool assume_connectivity =
true) {
62 CHECK(odd_nodes !=
nullptr);
65 if (degree % 2 != 0) {
66 odd_nodes->push_back(node);
69 if (odd_nodes->size() > 2)
return false;
78template <
typename NodeIndex,
typename Graph>
83 std::vector<uint8_t> unvisited_edges(graph.
num_arcs(),
true);
84 std::vector<NodeIndex> tour;
86 std::vector<NodeIndex> tour_stack = {root};
87 std::vector<ArcIndex> active_arcs(graph.
num_nodes());
91 while (!tour_stack.empty()) {
93 bool has_unvisited_edges =
false;
94 for (
const ArcIndex arc :
96 node, active_arcs[node])) {
97 const ArcIndex edge = arc < 0 ? graph.
OppositeArc(arc) : arc;
98 if (unvisited_edges[edge]) {
99 has_unvisited_edges =
true;
100 active_arcs[node] = arc;
101 tour_stack.push_back(graph.
Head(arc));
102 unvisited_edges[edge] =
false;
106 if (!has_unvisited_edges) {
107 tour.push_back(node);
108 tour_stack.pop_back();
120template <
typename NodeIndex,
typename Graph>
122 const Graph& graph,
NodeIndex root,
bool assume_connectivity =
true) {
123 std::vector<NodeIndex> tour;
132template <
typename Graph>
134 const Graph& graph,
bool assume_connectivity =
true) {
142template <
typename Graph>
144 const Graph& graph,
bool assume_connectivity =
true) {
146 std::vector<NodeIndex> path;
147 std::vector<NodeIndex> roots;
149 const NodeIndex root = roots.empty() ? 0 : roots.back();
156template <
typename Graph>
160 if (n <= 1)
return true;
163 std::vector<NodeIndex> stack = {0};
164 std::vector<bool> visited(n,
false);
165 while (!stack.empty()) {
170 if (!visited[neigh]) {
171 visited[neigh] =
true;
172 stack.push_back(neigh);
173 if (++num_visited == n)
return true;
NodeIndexType num_nodes() const
ArcIndexType num_arcs() const
IntegerRange< NodeIndex > AllNodes() const
bool IsNodeValid(NodeIndexType node) const
BeginEndWrapper< OutgoingOrOppositeIncomingArcIterator > OutgoingOrOppositeIncomingArcsStartingFrom(NodeIndexType node, ArcIndexType from) const
BeginEndWrapper< OutgoingOrOppositeIncomingArcIterator > OutgoingOrOppositeIncomingArcs(NodeIndexType node) const
ArcIndexType OppositeArc(ArcIndexType arc) const
NodeIndexType Head(ArcIndexType arc) const
ArcIndexType OutDegree(NodeIndexType node) const
ArcIndexType InDegree(NodeIndexType node) const
bool GraphIsConnected(const Graph &graph)
bool IsEulerianGraph(const Graph &graph, bool assume_connectivity=true)
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)
BlossomGraph::NodeIndex NodeIndex
util::ReverseArcStaticGraph Graph
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)