graph_algorithms_test.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #include "ceres/graph_algorithms.h"
  31. #include <algorithm>
  32. #include <memory>
  33. #include <unordered_set>
  34. #include <vector>
  35. #include "ceres/graph.h"
  36. #include "ceres/internal/export.h"
  37. #include "gtest/gtest.h"
  38. namespace ceres::internal {
  39. TEST(IndependentSetOrdering, Chain) {
  40. Graph<int> graph;
  41. graph.AddVertex(0);
  42. graph.AddVertex(1);
  43. graph.AddVertex(2);
  44. graph.AddVertex(3);
  45. graph.AddVertex(4);
  46. graph.AddEdge(0, 1);
  47. graph.AddEdge(1, 2);
  48. graph.AddEdge(2, 3);
  49. graph.AddEdge(3, 4);
  50. // 0-1-2-3-4
  51. // 0, 2, 4 should be in the independent set.
  52. std::vector<int> ordering;
  53. int independent_set_size = IndependentSetOrdering(graph, &ordering);
  54. sort(ordering.begin(), ordering.begin() + 3);
  55. sort(ordering.begin() + 3, ordering.end());
  56. EXPECT_EQ(independent_set_size, 3);
  57. EXPECT_EQ(ordering.size(), 5);
  58. EXPECT_EQ(ordering[0], 0);
  59. EXPECT_EQ(ordering[1], 2);
  60. EXPECT_EQ(ordering[2], 4);
  61. EXPECT_EQ(ordering[3], 1);
  62. EXPECT_EQ(ordering[4], 3);
  63. }
  64. TEST(IndependentSetOrdering, Star) {
  65. Graph<int> graph;
  66. graph.AddVertex(0);
  67. graph.AddVertex(1);
  68. graph.AddVertex(2);
  69. graph.AddVertex(3);
  70. graph.AddVertex(4);
  71. graph.AddEdge(0, 1);
  72. graph.AddEdge(0, 2);
  73. graph.AddEdge(0, 3);
  74. graph.AddEdge(0, 4);
  75. // 1
  76. // |
  77. // 4-0-2
  78. // |
  79. // 3
  80. // 1, 2, 3, 4 should be in the independent set.
  81. std::vector<int> ordering;
  82. int independent_set_size = IndependentSetOrdering(graph, &ordering);
  83. EXPECT_EQ(independent_set_size, 4);
  84. EXPECT_EQ(ordering.size(), 5);
  85. EXPECT_EQ(ordering[4], 0);
  86. sort(ordering.begin(), ordering.begin() + 4);
  87. EXPECT_EQ(ordering[0], 1);
  88. EXPECT_EQ(ordering[1], 2);
  89. EXPECT_EQ(ordering[2], 3);
  90. EXPECT_EQ(ordering[3], 4);
  91. }
  92. TEST(Degree2MaximumSpanningForest, PreserveWeights) {
  93. WeightedGraph<int> graph;
  94. graph.AddVertex(0, 1.0);
  95. graph.AddVertex(1, 2.0);
  96. graph.AddEdge(0, 1, 0.5);
  97. graph.AddEdge(1, 0, 0.5);
  98. std::unique_ptr<WeightedGraph<int>> forest(
  99. Degree2MaximumSpanningForest(graph));
  100. const std::unordered_set<int>& vertices = forest->vertices();
  101. EXPECT_EQ(vertices.size(), 2);
  102. EXPECT_EQ(forest->VertexWeight(0), 1.0);
  103. EXPECT_EQ(forest->VertexWeight(1), 2.0);
  104. EXPECT_EQ(forest->Neighbors(0).size(), 1.0);
  105. EXPECT_EQ(forest->EdgeWeight(0, 1), 0.5);
  106. }
  107. TEST(Degree2MaximumSpanningForest, StarGraph) {
  108. WeightedGraph<int> graph;
  109. graph.AddVertex(0);
  110. graph.AddVertex(1);
  111. graph.AddVertex(2);
  112. graph.AddVertex(3);
  113. graph.AddVertex(4);
  114. graph.AddEdge(0, 1, 1.0);
  115. graph.AddEdge(0, 2, 2.0);
  116. graph.AddEdge(0, 3, 3.0);
  117. graph.AddEdge(0, 4, 4.0);
  118. std::unique_ptr<WeightedGraph<int>> forest(
  119. Degree2MaximumSpanningForest(graph));
  120. const std::unordered_set<int>& vertices = forest->vertices();
  121. EXPECT_EQ(vertices.size(), 5);
  122. {
  123. const std::unordered_set<int>& neighbors = forest->Neighbors(0);
  124. EXPECT_EQ(neighbors.size(), 2);
  125. EXPECT_TRUE(neighbors.find(4) != neighbors.end());
  126. EXPECT_TRUE(neighbors.find(3) != neighbors.end());
  127. }
  128. {
  129. const std::unordered_set<int>& neighbors = forest->Neighbors(3);
  130. EXPECT_EQ(neighbors.size(), 1);
  131. EXPECT_TRUE(neighbors.find(0) != neighbors.end());
  132. }
  133. {
  134. const std::unordered_set<int>& neighbors = forest->Neighbors(4);
  135. EXPECT_EQ(neighbors.size(), 1);
  136. EXPECT_TRUE(neighbors.find(0) != neighbors.end());
  137. }
  138. {
  139. const std::unordered_set<int>& neighbors = forest->Neighbors(1);
  140. EXPECT_EQ(neighbors.size(), 0);
  141. }
  142. {
  143. const std::unordered_set<int>& neighbors = forest->Neighbors(2);
  144. EXPECT_EQ(neighbors.size(), 0);
  145. }
  146. }
  147. TEST(VertexTotalOrdering, TotalOrdering) {
  148. Graph<int> graph;
  149. graph.AddVertex(0);
  150. graph.AddVertex(1);
  151. graph.AddVertex(2);
  152. graph.AddVertex(3);
  153. // 0-1
  154. // |
  155. // 2-3
  156. // 0,1 and 2 have degree 1 and 3 has degree 2.
  157. graph.AddEdge(0, 1);
  158. graph.AddEdge(2, 3);
  159. VertexTotalOrdering<int> less_than(graph);
  160. for (int i = 0; i < 4; ++i) {
  161. EXPECT_FALSE(less_than(i, i)) << "Failing vertex: " << i;
  162. for (int j = 0; j < 4; ++j) {
  163. if (i != j) {
  164. EXPECT_TRUE(less_than(i, j) ^ less_than(j, i))
  165. << "Failing vertex pair: " << i << " " << j;
  166. }
  167. }
  168. }
  169. for (int i = 0; i < 3; ++i) {
  170. EXPECT_TRUE(less_than(i, 3));
  171. EXPECT_FALSE(less_than(3, i));
  172. }
  173. }
  174. TEST(StableIndependentSet, BreakTies) {
  175. Graph<int> graph;
  176. graph.AddVertex(0);
  177. graph.AddVertex(1);
  178. graph.AddVertex(2);
  179. graph.AddVertex(3);
  180. graph.AddEdge(0, 1);
  181. graph.AddEdge(0, 2);
  182. graph.AddEdge(0, 3);
  183. graph.AddEdge(1, 2);
  184. graph.AddEdge(1, 3);
  185. graph.AddEdge(2, 3);
  186. // Since this is a completely connected graph, the independent set
  187. // contains exactly one vertex. StableIndependentSetOrdering
  188. // guarantees that it will always be the first vertex in the
  189. // ordering vector.
  190. {
  191. std::vector<int> ordering;
  192. ordering.push_back(0);
  193. ordering.push_back(1);
  194. ordering.push_back(2);
  195. ordering.push_back(3);
  196. const int independent_set_size =
  197. StableIndependentSetOrdering(graph, &ordering);
  198. EXPECT_EQ(independent_set_size, 1);
  199. EXPECT_EQ(ordering[0], 0);
  200. }
  201. {
  202. std::vector<int> ordering;
  203. ordering.push_back(1);
  204. ordering.push_back(0);
  205. ordering.push_back(2);
  206. ordering.push_back(3);
  207. const int independent_set_size =
  208. StableIndependentSetOrdering(graph, &ordering);
  209. EXPECT_EQ(independent_set_size, 1);
  210. EXPECT_EQ(ordering[0], 1);
  211. }
  212. }
  213. } // namespace ceres::internal