forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_util.h
65 lines (57 loc) · 1.94 KB
/
test_util.h
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
// Copyright 2010-2025 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.
// Unit test utilities related to graph.h.
#ifndef UTIL_GRAPH_TEST_UTIL_H_
#define UTIL_GRAPH_TEST_UTIL_H_
#include <cstdint>
#include <memory>
#include "absl/memory/memory.h"
#include "ortools/base/types.h"
#include "ortools/graph/graph.h"
namespace util {
// Generate a 2-dimensional undirected grid graph.
//
// Eg. for width=3, height=2, it generates this:
// 0 <---> 1 <---> 2
// ^ ^ ^
// | | |
// v v v
// 3 <---> 4 <---> 5
template <class Graph>
std::unique_ptr<Graph> Create2DGridGraph(int64_t width, int64_t height) {
const int64_t num_arcs = 2L * ((width - 1) * height + width * (height - 1));
auto graph = std::make_unique<Graph>(/*num_nodes=*/width * height,
/*arc_capacity=*/num_arcs);
// Add horizontal edges.
for (int i = 0; i < height; ++i) {
for (int j = 1; j < width; ++j) {
const int left = i * width + (j - 1);
const int right = i * width + j;
graph->AddArc(left, right);
graph->AddArc(right, left);
}
}
// Add vertical edges.
for (int i = 1; i < height; ++i) {
for (int j = 0; j < width; ++j) {
const int up = (i - 1) * width + j;
const int down = i * width + j;
graph->AddArc(up, down);
graph->AddArc(down, up);
}
}
graph->Build();
return graph;
}
} // namespace util
#endif // UTIL_GRAPH_TEST_UTIL_H_