-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathminimum_vertex_cover_test.cc
95 lines (83 loc) · 3.28 KB
/
minimum_vertex_cover_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
// 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.
#include "ortools/graph/minimum_vertex_cover.h"
#include <vector>
#include "absl/algorithm/container.h"
#include "benchmark/benchmark.h"
#include "gtest/gtest.h"
namespace operations_research {
namespace {
// Creates the complete bipartite graph K(n, m).
std::vector<std::vector<int>> MakeCompleteBipartiteGraph(int num_left,
int num_right) {
std::vector<int> adjacencies(num_right);
absl::c_iota(adjacencies, num_left);
return std::vector<std::vector<int>>(num_left, adjacencies);
}
TEST(BipartiteMinimumVertexCoverTest, BasicBehavior) {
const int num_right = 4;
const std::vector<std::vector<int>> left_to_right = {
{5}, {4, 5, 6}, {5}, {5, 6, 7}};
const auto cover = BipartiteMinimumVertexCover(left_to_right, num_right);
EXPECT_EQ(absl::c_count(cover, true), 3);
EXPECT_EQ(absl::c_count(cover, false), 5);
}
TEST(BipartiteMinimumVertexCoverTest, StarGraph) {
const std::vector<std::vector<int>> left_to_right =
MakeCompleteBipartiteGraph(1, 4);
const auto cover = BipartiteMinimumVertexCover(left_to_right, 4);
EXPECT_EQ(absl::c_count(cover, true), 1);
EXPECT_EQ(absl::c_count(cover, false), 4);
}
TEST(BipartiteMinimumVertexCoverTest, UtilityGraph) {
const std::vector<std::vector<int>> left_to_right =
MakeCompleteBipartiteGraph(3, 3);
const auto cover = BipartiteMinimumVertexCover(left_to_right, 3);
EXPECT_EQ(absl::c_count(cover, true), 3);
EXPECT_EQ(absl::c_count(cover, false), 3);
}
TEST(BipartiteMinimumVertexCoverTest, DuplicateEdges) {
const int num_right = 4;
const std::vector<std::vector<int>> left_to_right = {
{5, 5}, {4, 4, 5, 6}, {5, 5, 5}, {5, 5, 5, 6, 6, 7}};
EXPECT_EQ(absl::c_count(BipartiteMinimumVertexCover(left_to_right, num_right),
true),
3);
EXPECT_EQ(absl::c_count(BipartiteMinimumVertexCover(left_to_right, num_right),
false),
5);
}
TEST(BipartiteMinimumVertexCoverTest, Empty) {
const int num_right = 4;
const std::vector<std::vector<int>> left_to_right = {{}, {}};
EXPECT_EQ(absl::c_count(BipartiteMinimumVertexCover(left_to_right, num_right),
false),
6);
}
void BM_CompleteBipartite(benchmark::State& state) {
const int num_left = state.range(0);
const int num_right = state.range(1);
std::vector<std::vector<int>> left_to_right =
MakeCompleteBipartiteGraph(num_left, num_right);
for (auto _ : state) {
BipartiteMinimumVertexCover(left_to_right, num_right);
}
}
BENCHMARK(BM_CompleteBipartite)
->ArgPair(1, 128)
->ArgPair(128, 1)
->ArgPair(32, 32)
->ArgPair(8, 64)
->ArgPair(64, 8);
} // namespace
} // namespace operations_research