29 #ifndef OR_TOOLS_GRAPH_EULERIAN_PATH_H_
30 #define OR_TOOLS_GRAPH_EULERIAN_PATH_H_
34 #include "ortools/base/logging.h"
39 template <
typename Graph>
44 template <
typename Graph>
47 for (
const NodeIndex node : graph.AllNodes()) {
48 if ((graph.OutDegree(node) + graph.InDegree(node)) % 2 != 0) {
58 template <
typename NodeIndex,
typename Graph>
60 bool assume_connectivity =
true) {
61 CHECK(odd_nodes !=
nullptr);
62 for (
const NodeIndex node : graph.AllNodes()) {
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;
77 template <
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());
86 for (
const NodeIndex node : graph.AllNodes()) {
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])) {
95 const ArcIndex edge = arc < 0 ? graph.OppositeArc(arc) : arc;
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();
118 template <
typename NodeIndex,
typename Graph>
120 const Graph& graph,
NodeIndex root,
bool assume_connectivity =
true) {
121 std::vector<NodeIndex> tour;
130 template <
typename Graph>
132 const Graph& graph,
bool assume_connectivity =
true) {
140 template <
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();
154 template <
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)
std::vector< typename Graph::NodeIndex > BuildEulerianPath(const Graph &graph, bool assume_connectivity=true)
bool IsEulerianGraph(const Graph &graph, bool assume_connectivity=true)
std::vector< NodeIndex > BuildEulerianTourFromNode(const Graph &graph, NodeIndex root, bool assume_connectivity=true)
std::vector< typename Graph::NodeIndex > BuildEulerianTour(const Graph &graph, bool assume_connectivity=true)
std::vector< NodeIndex > BuildEulerianPathFromNode(const Graph &graph, NodeIndex root)
bool IsSemiEulerianGraph(const Graph &graph, std::vector< NodeIndex > *odd_nodes, bool assume_connectivity=true)