graph.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 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. #ifndef CERES_INTERNAL_GRAPH_H_
  31. #define CERES_INTERNAL_GRAPH_H_
  32. #include <limits>
  33. #include <unordered_map>
  34. #include <unordered_set>
  35. #include <utility>
  36. #include "ceres/internal/export.h"
  37. #include "ceres/map_util.h"
  38. #include "ceres/pair_hash.h"
  39. #include "ceres/types.h"
  40. #include "glog/logging.h"
  41. namespace ceres::internal {
  42. // A unweighted undirected graph templated over the vertex ids. Vertex
  43. // should be hashable.
  44. template <typename Vertex>
  45. class CERES_NO_EXPORT Graph {
  46. public:
  47. // Add a vertex.
  48. void AddVertex(const Vertex& vertex) {
  49. if (vertices_.insert(vertex).second) {
  50. edges_[vertex] = std::unordered_set<Vertex>();
  51. }
  52. }
  53. bool RemoveVertex(const Vertex& vertex) {
  54. if (vertices_.find(vertex) == vertices_.end()) {
  55. return false;
  56. }
  57. vertices_.erase(vertex);
  58. const std::unordered_set<Vertex>& sinks = edges_[vertex];
  59. for (const Vertex& s : sinks) {
  60. edges_[s].erase(vertex);
  61. }
  62. edges_.erase(vertex);
  63. return true;
  64. }
  65. // Add an edge between the vertex1 and vertex2. Calling AddEdge on a
  66. // pair of vertices which do not exist in the graph yet will result
  67. // in undefined behavior.
  68. //
  69. // It is legal to call this method repeatedly for the same set of
  70. // vertices.
  71. void AddEdge(const Vertex& vertex1, const Vertex& vertex2) {
  72. DCHECK(vertices_.find(vertex1) != vertices_.end());
  73. DCHECK(vertices_.find(vertex2) != vertices_.end());
  74. if (edges_[vertex1].insert(vertex2).second) {
  75. edges_[vertex2].insert(vertex1);
  76. }
  77. }
  78. // Calling Neighbors on a vertex not in the graph will result in
  79. // undefined behaviour.
  80. const std::unordered_set<Vertex>& Neighbors(const Vertex& vertex) const {
  81. return FindOrDie(edges_, vertex);
  82. }
  83. const std::unordered_set<Vertex>& vertices() const { return vertices_; }
  84. private:
  85. std::unordered_set<Vertex> vertices_;
  86. std::unordered_map<Vertex, std::unordered_set<Vertex>> edges_;
  87. };
  88. // A weighted undirected graph templated over the vertex ids. Vertex
  89. // should be hashable and comparable.
  90. template <typename Vertex>
  91. class WeightedGraph {
  92. public:
  93. // Add a weighted vertex. If the vertex already exists in the graph,
  94. // its weight is set to the new weight.
  95. void AddVertex(const Vertex& vertex, double weight) {
  96. if (vertices_.find(vertex) == vertices_.end()) {
  97. vertices_.insert(vertex);
  98. edges_[vertex] = std::unordered_set<Vertex>();
  99. }
  100. vertex_weights_[vertex] = weight;
  101. }
  102. // Uses weight = 1.0. If vertex already exists, its weight is set to
  103. // 1.0.
  104. void AddVertex(const Vertex& vertex) { AddVertex(vertex, 1.0); }
  105. bool RemoveVertex(const Vertex& vertex) {
  106. if (vertices_.find(vertex) == vertices_.end()) {
  107. return false;
  108. }
  109. vertices_.erase(vertex);
  110. vertex_weights_.erase(vertex);
  111. const std::unordered_set<Vertex>& sinks = edges_[vertex];
  112. for (const Vertex& s : sinks) {
  113. if (vertex < s) {
  114. edge_weights_.erase(std::make_pair(vertex, s));
  115. } else {
  116. edge_weights_.erase(std::make_pair(s, vertex));
  117. }
  118. edges_[s].erase(vertex);
  119. }
  120. edges_.erase(vertex);
  121. return true;
  122. }
  123. // Add a weighted edge between the vertex1 and vertex2. Calling
  124. // AddEdge on a pair of vertices which do not exist in the graph yet
  125. // will result in undefined behavior.
  126. //
  127. // It is legal to call this method repeatedly for the same set of
  128. // vertices.
  129. void AddEdge(const Vertex& vertex1, const Vertex& vertex2, double weight) {
  130. DCHECK(vertices_.find(vertex1) != vertices_.end());
  131. DCHECK(vertices_.find(vertex2) != vertices_.end());
  132. if (edges_[vertex1].insert(vertex2).second) {
  133. edges_[vertex2].insert(vertex1);
  134. }
  135. if (vertex1 < vertex2) {
  136. edge_weights_[std::make_pair(vertex1, vertex2)] = weight;
  137. } else {
  138. edge_weights_[std::make_pair(vertex2, vertex1)] = weight;
  139. }
  140. }
  141. // Uses weight = 1.0.
  142. void AddEdge(const Vertex& vertex1, const Vertex& vertex2) {
  143. AddEdge(vertex1, vertex2, 1.0);
  144. }
  145. // Calling VertexWeight on a vertex not in the graph will result in
  146. // undefined behavior.
  147. double VertexWeight(const Vertex& vertex) const {
  148. return FindOrDie(vertex_weights_, vertex);
  149. }
  150. // Calling EdgeWeight on a pair of vertices where either one of the
  151. // vertices is not present in the graph will result in undefined
  152. // behaviour. If there is no edge connecting vertex1 and vertex2,
  153. // the edge weight is zero.
  154. double EdgeWeight(const Vertex& vertex1, const Vertex& vertex2) const {
  155. if (vertex1 < vertex2) {
  156. return FindWithDefault(
  157. edge_weights_, std::make_pair(vertex1, vertex2), 0.0);
  158. } else {
  159. return FindWithDefault(
  160. edge_weights_, std::make_pair(vertex2, vertex1), 0.0);
  161. }
  162. }
  163. // Calling Neighbors on a vertex not in the graph will result in
  164. // undefined behaviour.
  165. const std::unordered_set<Vertex>& Neighbors(const Vertex& vertex) const {
  166. return FindOrDie(edges_, vertex);
  167. }
  168. const std::unordered_set<Vertex>& vertices() const { return vertices_; }
  169. static double InvalidWeight() {
  170. return std::numeric_limits<double>::quiet_NaN();
  171. }
  172. private:
  173. std::unordered_set<Vertex> vertices_;
  174. std::unordered_map<Vertex, double> vertex_weights_;
  175. std::unordered_map<Vertex, std::unordered_set<Vertex>> edges_;
  176. std::unordered_map<std::pair<Vertex, Vertex>, double, pair_hash>
  177. edge_weights_;
  178. };
  179. } // namespace ceres::internal
  180. #endif // CERES_INTERNAL_GRAPH_H_