single_linkage_clustering_test.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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: Sameer Agarwal (sameeragarwal@google.com)
  30. #include "ceres/single_linkage_clustering.h"
  31. #include <unordered_map>
  32. #include "ceres/graph.h"
  33. #include "gtest/gtest.h"
  34. namespace ceres::internal {
  35. TEST(SingleLinkageClustering, GraphHasTwoComponents) {
  36. WeightedGraph<int> graph;
  37. const int kNumVertices = 6;
  38. for (int i = 0; i < kNumVertices; ++i) {
  39. graph.AddVertex(i);
  40. }
  41. // Graph structure:
  42. //
  43. // 0-1-2-3 4-5
  44. graph.AddEdge(0, 1, 1.0);
  45. graph.AddEdge(1, 2, 1.0);
  46. graph.AddEdge(2, 3, 1.0);
  47. graph.AddEdge(4, 5, 1.0);
  48. SingleLinkageClusteringOptions options;
  49. std::unordered_map<int, int> membership;
  50. ComputeSingleLinkageClustering(options, graph, &membership);
  51. EXPECT_EQ(membership.size(), kNumVertices);
  52. EXPECT_EQ(membership[1], membership[0]);
  53. EXPECT_EQ(membership[2], membership[0]);
  54. EXPECT_EQ(membership[3], membership[0]);
  55. EXPECT_NE(membership[4], membership[0]);
  56. EXPECT_NE(membership[5], membership[0]);
  57. EXPECT_EQ(membership[4], membership[5]);
  58. }
  59. TEST(SingleLinkageClustering, ComponentWithWeakLink) {
  60. WeightedGraph<int> graph;
  61. const int kNumVertices = 6;
  62. for (int i = 0; i < kNumVertices; ++i) {
  63. graph.AddVertex(i);
  64. }
  65. // Graph structure:
  66. //
  67. // 0-1-2-3 4-5
  68. graph.AddEdge(0, 1, 1.0);
  69. graph.AddEdge(1, 2, 1.0);
  70. graph.AddEdge(2, 3, 1.0);
  71. // This component should break up into two.
  72. graph.AddEdge(4, 5, 0.5);
  73. SingleLinkageClusteringOptions options;
  74. std::unordered_map<int, int> membership;
  75. ComputeSingleLinkageClustering(options, graph, &membership);
  76. EXPECT_EQ(membership.size(), kNumVertices);
  77. EXPECT_EQ(membership[1], membership[0]);
  78. EXPECT_EQ(membership[2], membership[0]);
  79. EXPECT_EQ(membership[3], membership[0]);
  80. EXPECT_NE(membership[4], membership[0]);
  81. EXPECT_NE(membership[5], membership[0]);
  82. EXPECT_NE(membership[4], membership[5]);
  83. }
  84. TEST(SingleLinkageClustering, ComponentWithWeakLinkAndStrongLink) {
  85. WeightedGraph<int> graph;
  86. const int kNumVertices = 6;
  87. for (int i = 0; i < kNumVertices; ++i) {
  88. graph.AddVertex(i);
  89. }
  90. // Graph structure:
  91. //
  92. // 0-1-2-3 4-5
  93. graph.AddEdge(0, 1, 1.0);
  94. graph.AddEdge(1, 2, 1.0);
  95. graph.AddEdge(2, 3, 0.5); // Weak link
  96. graph.AddEdge(0, 3, 1.0);
  97. // This component should break up into two.
  98. graph.AddEdge(4, 5, 1.0);
  99. SingleLinkageClusteringOptions options;
  100. std::unordered_map<int, int> membership;
  101. ComputeSingleLinkageClustering(options, graph, &membership);
  102. EXPECT_EQ(membership.size(), kNumVertices);
  103. EXPECT_EQ(membership[1], membership[0]);
  104. EXPECT_EQ(membership[2], membership[0]);
  105. EXPECT_EQ(membership[3], membership[0]);
  106. EXPECT_EQ(membership[4], membership[5]);
  107. }
  108. } // namespace ceres::internal