blob: d5dd02eb80ac4f1b25bfb9cddeee12181364403a [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2015 Google Inc. All rights reserved.
3// http://ceres-solver.org/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: sameeragarwal@google.com (Sameer Agarwal)
30
31#include "ceres/graph_algorithms.h"
32
33#include <algorithm>
34#include <memory>
35#include <unordered_set>
36
37#include "ceres/graph.h"
38#include "ceres/internal/port.h"
39#include "gtest/gtest.h"
40
41namespace ceres {
42namespace internal {
43
44using std::vector;
45
46TEST(IndependentSetOrdering, Chain) {
47 Graph<int> graph;
48 graph.AddVertex(0);
49 graph.AddVertex(1);
50 graph.AddVertex(2);
51 graph.AddVertex(3);
52 graph.AddVertex(4);
53
54 graph.AddEdge(0, 1);
55 graph.AddEdge(1, 2);
56 graph.AddEdge(2, 3);
57 graph.AddEdge(3, 4);
58
59 // 0-1-2-3-4
60 // 0, 2, 4 should be in the independent set.
61 vector<int> ordering;
62 int independent_set_size = IndependentSetOrdering(graph, &ordering);
63
64 sort(ordering.begin(), ordering.begin() + 3);
65 sort(ordering.begin() + 3, ordering.end());
66
67 EXPECT_EQ(independent_set_size, 3);
68 EXPECT_EQ(ordering.size(), 5);
69 EXPECT_EQ(ordering[0], 0);
70 EXPECT_EQ(ordering[1], 2);
71 EXPECT_EQ(ordering[2], 4);
72 EXPECT_EQ(ordering[3], 1);
73 EXPECT_EQ(ordering[4], 3);
74}
75
76TEST(IndependentSetOrdering, Star) {
77 Graph<int> graph;
78 graph.AddVertex(0);
79 graph.AddVertex(1);
80 graph.AddVertex(2);
81 graph.AddVertex(3);
82 graph.AddVertex(4);
83
84 graph.AddEdge(0, 1);
85 graph.AddEdge(0, 2);
86 graph.AddEdge(0, 3);
87 graph.AddEdge(0, 4);
88
89 // 1
90 // |
91 // 4-0-2
92 // |
93 // 3
94 // 1, 2, 3, 4 should be in the independent set.
95 vector<int> ordering;
96 int independent_set_size = IndependentSetOrdering(graph, &ordering);
97 EXPECT_EQ(independent_set_size, 4);
98 EXPECT_EQ(ordering.size(), 5);
99 EXPECT_EQ(ordering[4], 0);
100 sort(ordering.begin(), ordering.begin() + 4);
101 EXPECT_EQ(ordering[0], 1);
102 EXPECT_EQ(ordering[1], 2);
103 EXPECT_EQ(ordering[2], 3);
104 EXPECT_EQ(ordering[3], 4);
105}
106
107TEST(Degree2MaximumSpanningForest, PreserveWeights) {
108 WeightedGraph<int> graph;
109 graph.AddVertex(0, 1.0);
110 graph.AddVertex(1, 2.0);
111 graph.AddEdge(0, 1, 0.5);
112 graph.AddEdge(1, 0, 0.5);
113
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800114 std::unique_ptr<WeightedGraph<int>> forest(
115 Degree2MaximumSpanningForest(graph));
Austin Schuh70cc9552019-01-21 19:46:48 -0800116
117 const std::unordered_set<int>& vertices = forest->vertices();
118 EXPECT_EQ(vertices.size(), 2);
119 EXPECT_EQ(forest->VertexWeight(0), 1.0);
120 EXPECT_EQ(forest->VertexWeight(1), 2.0);
121 EXPECT_EQ(forest->Neighbors(0).size(), 1.0);
122 EXPECT_EQ(forest->EdgeWeight(0, 1), 0.5);
123}
124
125TEST(Degree2MaximumSpanningForest, StarGraph) {
126 WeightedGraph<int> graph;
127 graph.AddVertex(0);
128 graph.AddVertex(1);
129 graph.AddVertex(2);
130 graph.AddVertex(3);
131 graph.AddVertex(4);
132
133 graph.AddEdge(0, 1, 1.0);
134 graph.AddEdge(0, 2, 2.0);
135 graph.AddEdge(0, 3, 3.0);
136 graph.AddEdge(0, 4, 4.0);
137
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800138 std::unique_ptr<WeightedGraph<int>> forest(
139 Degree2MaximumSpanningForest(graph));
Austin Schuh70cc9552019-01-21 19:46:48 -0800140 const std::unordered_set<int>& vertices = forest->vertices();
141 EXPECT_EQ(vertices.size(), 5);
142
143 {
144 const std::unordered_set<int>& neighbors = forest->Neighbors(0);
145 EXPECT_EQ(neighbors.size(), 2);
146 EXPECT_TRUE(neighbors.find(4) != neighbors.end());
147 EXPECT_TRUE(neighbors.find(3) != neighbors.end());
148 }
149
150 {
151 const std::unordered_set<int>& neighbors = forest->Neighbors(3);
152 EXPECT_EQ(neighbors.size(), 1);
153 EXPECT_TRUE(neighbors.find(0) != neighbors.end());
154 }
155
156 {
157 const std::unordered_set<int>& neighbors = forest->Neighbors(4);
158 EXPECT_EQ(neighbors.size(), 1);
159 EXPECT_TRUE(neighbors.find(0) != neighbors.end());
160 }
161
162 {
163 const std::unordered_set<int>& neighbors = forest->Neighbors(1);
164 EXPECT_EQ(neighbors.size(), 0);
165 }
166
167 {
168 const std::unordered_set<int>& neighbors = forest->Neighbors(2);
169 EXPECT_EQ(neighbors.size(), 0);
170 }
171}
172
173TEST(VertexTotalOrdering, TotalOrdering) {
174 Graph<int> graph;
175 graph.AddVertex(0);
176 graph.AddVertex(1);
177 graph.AddVertex(2);
178 graph.AddVertex(3);
179
180 // 0-1
181 // |
182 // 2-3
183 // 0,1 and 2 have degree 1 and 3 has degree 2.
184 graph.AddEdge(0, 1);
185 graph.AddEdge(2, 3);
186 VertexTotalOrdering<int> less_than(graph);
187
188 for (int i = 0; i < 4; ++i) {
189 EXPECT_FALSE(less_than(i, i)) << "Failing vertex: " << i;
190 for (int j = 0; j < 4; ++j) {
191 if (i != j) {
192 EXPECT_TRUE(less_than(i, j) ^ less_than(j, i))
193 << "Failing vertex pair: " << i << " " << j;
194 }
195 }
196 }
197
198 for (int i = 0; i < 3; ++i) {
199 EXPECT_TRUE(less_than(i, 3));
200 EXPECT_FALSE(less_than(3, i));
201 }
202}
203
Austin Schuh70cc9552019-01-21 19:46:48 -0800204TEST(StableIndependentSet, BreakTies) {
205 Graph<int> graph;
206 graph.AddVertex(0);
207 graph.AddVertex(1);
208 graph.AddVertex(2);
209 graph.AddVertex(3);
210
211 graph.AddEdge(0, 1);
212 graph.AddEdge(0, 2);
213 graph.AddEdge(0, 3);
214 graph.AddEdge(1, 2);
215 graph.AddEdge(1, 3);
216 graph.AddEdge(2, 3);
217
218 // Since this is a completely connected graph, the independent set
219 // contains exactly one vertex. StableIndependentSetOrdering
220 // guarantees that it will always be the first vertex in the
221 // ordering vector.
222 {
223 vector<int> ordering;
224 ordering.push_back(0);
225 ordering.push_back(1);
226 ordering.push_back(2);
227 ordering.push_back(3);
228 const int independent_set_size =
229 StableIndependentSetOrdering(graph, &ordering);
230 EXPECT_EQ(independent_set_size, 1);
231 EXPECT_EQ(ordering[0], 0);
232 }
233
234 {
235 vector<int> ordering;
236 ordering.push_back(1);
237 ordering.push_back(0);
238 ordering.push_back(2);
239 ordering.push_back(3);
240 const int independent_set_size =
241 StableIndependentSetOrdering(graph, &ordering);
242 EXPECT_EQ(independent_set_size, 1);
243 EXPECT_EQ(ordering[0], 1);
244 }
245}
246
247} // namespace internal
248} // namespace ceres