forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 9
/
eulerian_path_test.cc
249 lines (226 loc) · 8.29 KB
/
eulerian_path_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2010-2024 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/graph/eulerian_path.h"
#include <vector>
#include "benchmark/benchmark.h"
#include "gtest/gtest.h"
#include "ortools/base/gmock.h"
#include "ortools/base/logging.h"
#include "ortools/base/macros.h"
#include "ortools/graph/graph.h"
namespace operations_research {
namespace {
using ::testing::ElementsAre;
using ::testing::IsEmpty;
void TestTour(const int arcs[][2], int num_nodes, int num_arcs, int root,
bool eulerian, const int expected_tour[]) {
util::ReverseArcListGraph<int, int> graph(num_nodes, num_arcs);
for (int i = 0; i < num_arcs; ++i) {
graph.AddArc(arcs[i][0], arcs[i][1]);
}
EXPECT_EQ(eulerian, IsEulerianGraph(graph));
const std::vector<int> tour = root < 0
? BuildEulerianTour(graph)
: BuildEulerianTourFromNode(graph, root);
EXPECT_EQ(tour.size(), (eulerian && num_nodes != 0) ? num_arcs + 1 : 0);
for (int i = 0; i < tour.size(); ++i) {
EXPECT_EQ(expected_tour[i], tour[i]);
}
}
void TestPath(const int arcs[][2], int num_nodes, int num_arcs, bool eulerian,
const int expected_path[]) {
util::ReverseArcListGraph<int, int> graph(num_nodes, num_arcs);
for (int i = 0; i < num_arcs; ++i) {
graph.AddArc(arcs[i][0], arcs[i][1]);
}
std::vector<int> odd_nodes;
EXPECT_EQ(eulerian, IsSemiEulerianGraph(graph, &odd_nodes));
const std::vector<int> path = BuildEulerianPath(graph);
EXPECT_EQ(path.size(), num_nodes != 0 ? num_arcs + 1 : 0);
for (int i = 0; i < path.size(); ++i) {
EXPECT_EQ(expected_path[i], path[i]) << i;
}
}
TEST(EulerianTourTest, EmptyGraph) {
const auto kArcs = nullptr;
const auto kExpectedTour = nullptr;
TestTour(kArcs, 0, 0, -1, true, kExpectedTour);
}
// Builds a tour on the following graph:
// 0---------1
// | |
// | |
// | |
// 3---------2
//
TEST(EulerianTourTest, SimpleCycle) {
const int kArcs[][2] = {{0, 1}, {0, 3}, {1, 2}, {2, 3}};
const int kExpectedTour[] = {0, 1, 2, 3, 0};
TestTour(kArcs, 4, ABSL_ARRAYSIZE(kArcs), 0, true, kExpectedTour);
TestTour(kArcs, 4, ABSL_ARRAYSIZE(kArcs), -1, true, kExpectedTour);
}
// Builds a tour starting at 1 on the following graph:
// 0---------1
// | /|\
// | 4 | 5
// | \|/
// 3---------2
//
TEST(EulerianTourTest, MultiCycle) {
const int kArcs[][2] = {{0, 1}, {1, 2}, {1, 4}, {1, 5},
{2, 3}, {2, 4}, {2, 5}, {3, 0}};
const int kExpectedTour[] = {1, 4, 2, 5, 1, 2, 3, 0, 1};
TestTour(kArcs, 6, ABSL_ARRAYSIZE(kArcs), 1, true, kExpectedTour);
}
// Fails to build a tour on the following graph:
// 0---------1
// | / \
// | 4 5
// | \ /
// 3---------2
//
TEST(EulerianTourTest, NonEulerian) {
const int kArcs[][2] = {{0, 1}, {1, 4}, {1, 5}, {2, 3},
{2, 4}, {2, 5}, {3, 0}};
const auto kExpectedTour = nullptr;
TestTour(kArcs, 6, ABSL_ARRAYSIZE(kArcs), 1, false, kExpectedTour);
}
TEST(EulerianPathTest, EmptyGraph) {
const auto kArcs = nullptr;
const auto kExpectedPath = nullptr;
TestPath(kArcs, 0, 0, true, kExpectedPath);
}
// Builds a path on the following graph:
// 0---------1
// | /|\
// | 4 | 5
// | \|/
// 3---------2
//
TEST(EulerianPathTest, MultiCycle) {
const int kArcs[][2] = {{0, 1}, {1, 2}, {1, 4}, {1, 5},
{2, 3}, {2, 4}, {2, 5}, {3, 0}};
const int kExpectedPath[] = {0, 1, 4, 2, 5, 1, 2, 3, 0};
TestPath(kArcs, 6, ABSL_ARRAYSIZE(kArcs), true, kExpectedPath);
}
// Builds a path on the following graph:
// 0---3
// | /|
// | / |
// |/ |
// 1---2
// |
// 4
TEST(EulerianPathTest, TwoOddNodes1) {
const int kArcs[][2] = {{0, 1}, {0, 3}, {1, 2}, {1, 3}, {1, 4}, {2, 3}};
const int kExpectedPath[] = {3, 1, 2, 3, 0, 1, 4};
TestPath(kArcs, 5, ABSL_ARRAYSIZE(kArcs), true, kExpectedPath);
}
// Builds a path on the following graph:
// 5
// / \
// 0---4
// |\ /|
// | X |
// |/ \|
// 1---2
// | |
// 6 3
TEST(EulerianPathTest, TwoOddNodes2) {
const int kArcs[][2] = {{0, 1}, {0, 2}, {0, 4}, {0, 5}, {1, 2},
{1, 4}, {1, 6}, {2, 3}, {2, 4}, {4, 5}};
const int kExpectedPath[] = {3, 2, 0, 4, 1, 2, 4, 5, 0, 1, 6};
TestPath(kArcs, 7, ABSL_ARRAYSIZE(kArcs), true, kExpectedPath);
}
TEST(EulerianPathTest, Disconnected) {
// Graph: 0===1 2===3. Would be Eulerian if connected.
const int kArcs[][2] = {{0, 1}, {1, 0}, {2, 3}, {3, 2}};
util::ReverseArcListGraph<int, int> graph(4, ABSL_ARRAYSIZE(kArcs));
for (const auto& [from, to] : kArcs) {
graph.AddArc(from, to);
}
std::vector<int> odd_nodes;
// If we do *not* assume the connectivity, we detect that it's disconnected
// and see that it's not Eulerian.
EXPECT_FALSE(IsEulerianGraph(graph, /*assume_connectivity=*/false));
EXPECT_FALSE(
IsSemiEulerianGraph(graph, &odd_nodes, /*assume_connectivity=*/false));
EXPECT_THAT(BuildEulerianTour(graph, /*assume_connectivity=*/false),
IsEmpty());
EXPECT_THAT(BuildEulerianPath(graph, /*assume_connectivity=*/false),
IsEmpty());
// If we assume the connectivity, we do not detect that it's disconnected
// and we think it's Eulerian.
EXPECT_TRUE(IsEulerianGraph(graph, /*assume_connectivity=*/true));
EXPECT_TRUE(
IsSemiEulerianGraph(graph, &odd_nodes, /*assume_connectivity=*/true));
EXPECT_THAT(BuildEulerianTour(graph, /*assume_connectivity=*/true),
ElementsAre(0, 1, 0));
EXPECT_THAT(BuildEulerianPath(graph, /*assume_connectivity=*/true),
ElementsAre(0, 1, 0));
// Test that the default is "assume connectivity".
EXPECT_EQ(IsEulerianGraph(graph), IsEulerianGraph(graph, true));
EXPECT_NE(IsEulerianGraph(graph), IsEulerianGraph(graph, false));
}
TEST(EulerianPathTest, EulerianPathWithSuccessfulConnectivityCheck) {
// Graph entered as 0-->1<--2, but direction doesn't matter.
const int kArcs[][2] = {{0, 1}, {1, 2}};
util::ReverseArcListGraph<int, int> graph(3, ABSL_ARRAYSIZE(kArcs));
for (const auto& [from, to] : kArcs) {
graph.AddArc(from, to);
}
std::vector<int> odd_nodes;
EXPECT_TRUE(
IsSemiEulerianGraph(graph, &odd_nodes, /*assume_connectivity=*/false));
EXPECT_THAT(BuildEulerianPath(graph, /*assume_connectivity=*/false),
ElementsAre(0, 1, 2));
}
TEST(EulerianPathTest, EulerianTourWithSuccessfulConnectivityCheck) {
// Graph: 0===1.
const int kArcs[][2] = {{0, 1}, {1, 0}};
util::ReverseArcListGraph<int, int> graph(2, ABSL_ARRAYSIZE(kArcs));
for (const auto& [from, to] : kArcs) {
graph.AddArc(from, to);
}
EXPECT_TRUE(IsEulerianGraph(graph, /*assume_connectivity=*/false));
EXPECT_THAT(BuildEulerianTour(graph, /*assume_connectivity=*/false),
ElementsAre(0, 1, 0));
}
template <typename GraphType>
static void BM_EulerianTourOnGrid(benchmark::State& state) {
int size = state.range(0);
const int num_nodes = size * size;
const int num_edges = 2 * size * (size - 1) + 2 * size - 4;
util::ReverseArcListGraph<int, int> graph(num_nodes, num_edges);
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
if (j < size - 1) {
graph.AddArc(i * size + j, i * size + j + 1);
}
if (i < size - 1) {
graph.AddArc(i * size + j, (i + 1) * size + j);
}
}
}
for (int i = 1; i < size - 1; ++i) {
graph.AddArc(i * size, i * size + size - 1);
graph.AddArc(i, (size - 1) * size + i);
}
for (auto _ : state) {
ASSERT_EQ(num_edges + 1, BuildEulerianTour(graph).size());
}
}
BENCHMARK_TEMPLATE(BM_EulerianTourOnGrid, util::ReverseArcStaticGraph<>)
->Range(2, 1 << 10);
} // namespace
} // namespace operations_research