Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame^] | 1 | // 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 | |
| 41 | namespace ceres { |
| 42 | namespace internal { |
| 43 | |
| 44 | using std::vector; |
| 45 | |
| 46 | TEST(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 | |
| 76 | TEST(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 | |
| 107 | TEST(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 | |
| 114 | std::unique_ptr<WeightedGraph<int> > forest( |
| 115 | Degree2MaximumSpanningForest(graph)); |
| 116 | |
| 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 | |
| 125 | TEST(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 | |
| 138 | std::unique_ptr<WeightedGraph<int> > forest(Degree2MaximumSpanningForest(graph)); |
| 139 | const std::unordered_set<int>& vertices = forest->vertices(); |
| 140 | EXPECT_EQ(vertices.size(), 5); |
| 141 | |
| 142 | { |
| 143 | const std::unordered_set<int>& neighbors = forest->Neighbors(0); |
| 144 | EXPECT_EQ(neighbors.size(), 2); |
| 145 | EXPECT_TRUE(neighbors.find(4) != neighbors.end()); |
| 146 | EXPECT_TRUE(neighbors.find(3) != neighbors.end()); |
| 147 | } |
| 148 | |
| 149 | { |
| 150 | const std::unordered_set<int>& neighbors = forest->Neighbors(3); |
| 151 | EXPECT_EQ(neighbors.size(), 1); |
| 152 | EXPECT_TRUE(neighbors.find(0) != neighbors.end()); |
| 153 | } |
| 154 | |
| 155 | { |
| 156 | const std::unordered_set<int>& neighbors = forest->Neighbors(4); |
| 157 | EXPECT_EQ(neighbors.size(), 1); |
| 158 | EXPECT_TRUE(neighbors.find(0) != neighbors.end()); |
| 159 | } |
| 160 | |
| 161 | { |
| 162 | const std::unordered_set<int>& neighbors = forest->Neighbors(1); |
| 163 | EXPECT_EQ(neighbors.size(), 0); |
| 164 | } |
| 165 | |
| 166 | { |
| 167 | const std::unordered_set<int>& neighbors = forest->Neighbors(2); |
| 168 | EXPECT_EQ(neighbors.size(), 0); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | TEST(VertexTotalOrdering, TotalOrdering) { |
| 173 | Graph<int> graph; |
| 174 | graph.AddVertex(0); |
| 175 | graph.AddVertex(1); |
| 176 | graph.AddVertex(2); |
| 177 | graph.AddVertex(3); |
| 178 | |
| 179 | // 0-1 |
| 180 | // | |
| 181 | // 2-3 |
| 182 | // 0,1 and 2 have degree 1 and 3 has degree 2. |
| 183 | graph.AddEdge(0, 1); |
| 184 | graph.AddEdge(2, 3); |
| 185 | VertexTotalOrdering<int> less_than(graph); |
| 186 | |
| 187 | for (int i = 0; i < 4; ++i) { |
| 188 | EXPECT_FALSE(less_than(i, i)) << "Failing vertex: " << i; |
| 189 | for (int j = 0; j < 4; ++j) { |
| 190 | if (i != j) { |
| 191 | EXPECT_TRUE(less_than(i, j) ^ less_than(j, i)) |
| 192 | << "Failing vertex pair: " << i << " " << j; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | for (int i = 0; i < 3; ++i) { |
| 198 | EXPECT_TRUE(less_than(i, 3)); |
| 199 | EXPECT_FALSE(less_than(3, i)); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | |
| 204 | TEST(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 |