29#ifndef OR_TOOLS_GRAPH_EULERIAN_PATH_H_
30#define OR_TOOLS_GRAPH_EULERIAN_PATH_H_
39template <
typename Graph>
44template <
typename Graph>
58template <
typename NodeIndex,
typename Graph>
60 bool assume_connectivity =
true) {
61 CHECK(odd_nodes !=
nullptr);
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;
84 std::vector<NodeIndex> tour_stack = {root};
85 std::vector<ArcIndex> active_arcs(graph.
num_nodes());
89 while (!tour_stack.empty()) {
91 bool has_unvisited_edges =
false;
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>
120 const Graph& graph,
NodeIndex root,
bool assume_connectivity =
true) {
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()) {
168 if (!visited[neigh]) {
169 visited[neigh] =
true;
170 stack.push_back(neigh);
171 if (++num_visited == n)
return true;
NodeIndexType num_nodes() const
ArcIndexType num_arcs() const
Returns the number of valid arcs in the graph.
IntegerRange< NodeIndex > AllNodes() const
BaseGraph implementation -------------------------------------------------—.
bool IsNodeValid(NodeIndexType node) const
Returns true if the given node is a valid node of the graph.
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
ReverseArcStaticGraph<>::OutDegree() and InDegree() work in O(1).
ArcIndexType InDegree(NodeIndexType node) const
End of the interface. Below is the implementation.
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)