blob: 6c86668f12d01d78bdd8772ca8cfedc70b9b8fae [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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>
Austin Schuh3de38b02024-06-25 18:25:10 -070036#include <vector>
Austin Schuh70cc9552019-01-21 19:46:48 -080037
38#include "ceres/graph.h"
Austin Schuh3de38b02024-06-25 18:25:10 -070039#include "ceres/internal/export.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080040#include "gtest/gtest.h"
41
Austin Schuh3de38b02024-06-25 18:25:10 -070042namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080043
44TEST(IndependentSetOrdering, Chain) {
45 Graph<int> graph;
46 graph.AddVertex(0);
47 graph.AddVertex(1);
48 graph.AddVertex(2);
49 graph.AddVertex(3);
50 graph.AddVertex(4);
51
52 graph.AddEdge(0, 1);
53 graph.AddEdge(1, 2);
54 graph.AddEdge(2, 3);
55 graph.AddEdge(3, 4);
56
57 // 0-1-2-3-4
58 // 0, 2, 4 should be in the independent set.
Austin Schuh3de38b02024-06-25 18:25:10 -070059 std::vector<int> ordering;
Austin Schuh70cc9552019-01-21 19:46:48 -080060 int independent_set_size = IndependentSetOrdering(graph, &ordering);
61
62 sort(ordering.begin(), ordering.begin() + 3);
63 sort(ordering.begin() + 3, ordering.end());
64
65 EXPECT_EQ(independent_set_size, 3);
66 EXPECT_EQ(ordering.size(), 5);
67 EXPECT_EQ(ordering[0], 0);
68 EXPECT_EQ(ordering[1], 2);
69 EXPECT_EQ(ordering[2], 4);
70 EXPECT_EQ(ordering[3], 1);
71 EXPECT_EQ(ordering[4], 3);
72}
73
74TEST(IndependentSetOrdering, Star) {
75 Graph<int> graph;
76 graph.AddVertex(0);
77 graph.AddVertex(1);
78 graph.AddVertex(2);
79 graph.AddVertex(3);
80 graph.AddVertex(4);
81
82 graph.AddEdge(0, 1);
83 graph.AddEdge(0, 2);
84 graph.AddEdge(0, 3);
85 graph.AddEdge(0, 4);
86
87 // 1
88 // |
89 // 4-0-2
90 // |
91 // 3
92 // 1, 2, 3, 4 should be in the independent set.
Austin Schuh3de38b02024-06-25 18:25:10 -070093 std::vector<int> ordering;
Austin Schuh70cc9552019-01-21 19:46:48 -080094 int independent_set_size = IndependentSetOrdering(graph, &ordering);
95 EXPECT_EQ(independent_set_size, 4);
96 EXPECT_EQ(ordering.size(), 5);
97 EXPECT_EQ(ordering[4], 0);
98 sort(ordering.begin(), ordering.begin() + 4);
99 EXPECT_EQ(ordering[0], 1);
100 EXPECT_EQ(ordering[1], 2);
101 EXPECT_EQ(ordering[2], 3);
102 EXPECT_EQ(ordering[3], 4);
103}
104
105TEST(Degree2MaximumSpanningForest, PreserveWeights) {
106 WeightedGraph<int> graph;
107 graph.AddVertex(0, 1.0);
108 graph.AddVertex(1, 2.0);
109 graph.AddEdge(0, 1, 0.5);
110 graph.AddEdge(1, 0, 0.5);
111
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800112 std::unique_ptr<WeightedGraph<int>> forest(
113 Degree2MaximumSpanningForest(graph));
Austin Schuh70cc9552019-01-21 19:46:48 -0800114
115 const std::unordered_set<int>& vertices = forest->vertices();
116 EXPECT_EQ(vertices.size(), 2);
117 EXPECT_EQ(forest->VertexWeight(0), 1.0);
118 EXPECT_EQ(forest->VertexWeight(1), 2.0);
119 EXPECT_EQ(forest->Neighbors(0).size(), 1.0);
120 EXPECT_EQ(forest->EdgeWeight(0, 1), 0.5);
121}
122
123TEST(Degree2MaximumSpanningForest, StarGraph) {
124 WeightedGraph<int> graph;
125 graph.AddVertex(0);
126 graph.AddVertex(1);
127 graph.AddVertex(2);
128 graph.AddVertex(3);
129 graph.AddVertex(4);
130
131 graph.AddEdge(0, 1, 1.0);
132 graph.AddEdge(0, 2, 2.0);
133 graph.AddEdge(0, 3, 3.0);
134 graph.AddEdge(0, 4, 4.0);
135
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800136 std::unique_ptr<WeightedGraph<int>> forest(
137 Degree2MaximumSpanningForest(graph));
Austin Schuh70cc9552019-01-21 19:46:48 -0800138 const std::unordered_set<int>& vertices = forest->vertices();
139 EXPECT_EQ(vertices.size(), 5);
140
141 {
142 const std::unordered_set<int>& neighbors = forest->Neighbors(0);
143 EXPECT_EQ(neighbors.size(), 2);
144 EXPECT_TRUE(neighbors.find(4) != neighbors.end());
145 EXPECT_TRUE(neighbors.find(3) != neighbors.end());
146 }
147
148 {
149 const std::unordered_set<int>& neighbors = forest->Neighbors(3);
150 EXPECT_EQ(neighbors.size(), 1);
151 EXPECT_TRUE(neighbors.find(0) != neighbors.end());
152 }
153
154 {
155 const std::unordered_set<int>& neighbors = forest->Neighbors(4);
156 EXPECT_EQ(neighbors.size(), 1);
157 EXPECT_TRUE(neighbors.find(0) != neighbors.end());
158 }
159
160 {
161 const std::unordered_set<int>& neighbors = forest->Neighbors(1);
162 EXPECT_EQ(neighbors.size(), 0);
163 }
164
165 {
166 const std::unordered_set<int>& neighbors = forest->Neighbors(2);
167 EXPECT_EQ(neighbors.size(), 0);
168 }
169}
170
171TEST(VertexTotalOrdering, TotalOrdering) {
172 Graph<int> graph;
173 graph.AddVertex(0);
174 graph.AddVertex(1);
175 graph.AddVertex(2);
176 graph.AddVertex(3);
177
178 // 0-1
179 // |
180 // 2-3
181 // 0,1 and 2 have degree 1 and 3 has degree 2.
182 graph.AddEdge(0, 1);
183 graph.AddEdge(2, 3);
184 VertexTotalOrdering<int> less_than(graph);
185
186 for (int i = 0; i < 4; ++i) {
187 EXPECT_FALSE(less_than(i, i)) << "Failing vertex: " << i;
188 for (int j = 0; j < 4; ++j) {
189 if (i != j) {
190 EXPECT_TRUE(less_than(i, j) ^ less_than(j, i))
191 << "Failing vertex pair: " << i << " " << j;
192 }
193 }
194 }
195
196 for (int i = 0; i < 3; ++i) {
197 EXPECT_TRUE(less_than(i, 3));
198 EXPECT_FALSE(less_than(3, i));
199 }
200}
201
Austin Schuh70cc9552019-01-21 19:46:48 -0800202TEST(StableIndependentSet, BreakTies) {
203 Graph<int> graph;
204 graph.AddVertex(0);
205 graph.AddVertex(1);
206 graph.AddVertex(2);
207 graph.AddVertex(3);
208
209 graph.AddEdge(0, 1);
210 graph.AddEdge(0, 2);
211 graph.AddEdge(0, 3);
212 graph.AddEdge(1, 2);
213 graph.AddEdge(1, 3);
214 graph.AddEdge(2, 3);
215
216 // Since this is a completely connected graph, the independent set
217 // contains exactly one vertex. StableIndependentSetOrdering
218 // guarantees that it will always be the first vertex in the
219 // ordering vector.
220 {
Austin Schuh3de38b02024-06-25 18:25:10 -0700221 std::vector<int> ordering;
Austin Schuh70cc9552019-01-21 19:46:48 -0800222 ordering.push_back(0);
223 ordering.push_back(1);
224 ordering.push_back(2);
225 ordering.push_back(3);
226 const int independent_set_size =
227 StableIndependentSetOrdering(graph, &ordering);
228 EXPECT_EQ(independent_set_size, 1);
229 EXPECT_EQ(ordering[0], 0);
230 }
231
232 {
Austin Schuh3de38b02024-06-25 18:25:10 -0700233 std::vector<int> ordering;
Austin Schuh70cc9552019-01-21 19:46:48 -0800234 ordering.push_back(1);
235 ordering.push_back(0);
236 ordering.push_back(2);
237 ordering.push_back(3);
238 const int independent_set_size =
239 StableIndependentSetOrdering(graph, &ordering);
240 EXPECT_EQ(independent_set_size, 1);
241 EXPECT_EQ(ordering[0], 1);
242 }
243}
244
Austin Schuh3de38b02024-06-25 18:25:10 -0700245} // namespace ceres::internal