blob: 9b26158753f34b2b8c901a6af93d0ddae90df99e [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// 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#ifndef CERES_INTERNAL_GRAPH_H_
32#define CERES_INTERNAL_GRAPH_H_
33
34#include <limits>
Austin Schuh70cc9552019-01-21 19:46:48 -080035#include <unordered_map>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080036#include <unordered_set>
Austin Schuh70cc9552019-01-21 19:46:48 -080037#include <utility>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080038
Austin Schuh70cc9552019-01-21 19:46:48 -080039#include "ceres/map_util.h"
40#include "ceres/pair_hash.h"
41#include "ceres/types.h"
42#include "glog/logging.h"
43
44namespace ceres {
45namespace internal {
46
47// A unweighted undirected graph templated over the vertex ids. Vertex
48// should be hashable.
49template <typename Vertex>
50class Graph {
51 public:
52 Graph() {}
53
54 // Add a vertex.
55 void AddVertex(const Vertex& vertex) {
56 if (vertices_.insert(vertex).second) {
57 edges_[vertex] = std::unordered_set<Vertex>();
58 }
59 }
60
61 bool RemoveVertex(const Vertex& vertex) {
62 if (vertices_.find(vertex) == vertices_.end()) {
63 return false;
64 }
65
66 vertices_.erase(vertex);
67 const std::unordered_set<Vertex>& sinks = edges_[vertex];
68 for (const Vertex& s : sinks) {
69 edges_[s].erase(vertex);
70 }
71
72 edges_.erase(vertex);
73 return true;
74 }
75
76 // Add an edge between the vertex1 and vertex2. Calling AddEdge on a
77 // pair of vertices which do not exist in the graph yet will result
78 // in undefined behavior.
79 //
80 // It is legal to call this method repeatedly for the same set of
81 // vertices.
82 void AddEdge(const Vertex& vertex1, const Vertex& vertex2) {
83 DCHECK(vertices_.find(vertex1) != vertices_.end());
84 DCHECK(vertices_.find(vertex2) != vertices_.end());
85
86 if (edges_[vertex1].insert(vertex2).second) {
87 edges_[vertex2].insert(vertex1);
88 }
89 }
90
91 // Calling Neighbors on a vertex not in the graph will result in
92 // undefined behaviour.
93 const std::unordered_set<Vertex>& Neighbors(const Vertex& vertex) const {
94 return FindOrDie(edges_, vertex);
95 }
96
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080097 const std::unordered_set<Vertex>& vertices() const { return vertices_; }
Austin Schuh70cc9552019-01-21 19:46:48 -080098
99 private:
100 std::unordered_set<Vertex> vertices_;
101 std::unordered_map<Vertex, std::unordered_set<Vertex>> edges_;
102};
103
104// A weighted undirected graph templated over the vertex ids. Vertex
105// should be hashable and comparable.
106template <typename Vertex>
107class WeightedGraph {
108 public:
109 WeightedGraph() {}
110
111 // Add a weighted vertex. If the vertex already exists in the graph,
112 // its weight is set to the new weight.
113 void AddVertex(const Vertex& vertex, double weight) {
114 if (vertices_.find(vertex) == vertices_.end()) {
115 vertices_.insert(vertex);
116 edges_[vertex] = std::unordered_set<Vertex>();
117 }
118 vertex_weights_[vertex] = weight;
119 }
120
121 // Uses weight = 1.0. If vertex already exists, its weight is set to
122 // 1.0.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800123 void AddVertex(const Vertex& vertex) { AddVertex(vertex, 1.0); }
Austin Schuh70cc9552019-01-21 19:46:48 -0800124
125 bool RemoveVertex(const Vertex& vertex) {
126 if (vertices_.find(vertex) == vertices_.end()) {
127 return false;
128 }
129
130 vertices_.erase(vertex);
131 vertex_weights_.erase(vertex);
132 const std::unordered_set<Vertex>& sinks = edges_[vertex];
133 for (const Vertex& s : sinks) {
134 if (vertex < s) {
135 edge_weights_.erase(std::make_pair(vertex, s));
136 } else {
137 edge_weights_.erase(std::make_pair(s, vertex));
138 }
139 edges_[s].erase(vertex);
140 }
141
142 edges_.erase(vertex);
143 return true;
144 }
145
146 // Add a weighted edge between the vertex1 and vertex2. Calling
147 // AddEdge on a pair of vertices which do not exist in the graph yet
148 // will result in undefined behavior.
149 //
150 // It is legal to call this method repeatedly for the same set of
151 // vertices.
152 void AddEdge(const Vertex& vertex1, const Vertex& vertex2, double weight) {
153 DCHECK(vertices_.find(vertex1) != vertices_.end());
154 DCHECK(vertices_.find(vertex2) != vertices_.end());
155
156 if (edges_[vertex1].insert(vertex2).second) {
157 edges_[vertex2].insert(vertex1);
158 }
159
160 if (vertex1 < vertex2) {
161 edge_weights_[std::make_pair(vertex1, vertex2)] = weight;
162 } else {
163 edge_weights_[std::make_pair(vertex2, vertex1)] = weight;
164 }
165 }
166
167 // Uses weight = 1.0.
168 void AddEdge(const Vertex& vertex1, const Vertex& vertex2) {
169 AddEdge(vertex1, vertex2, 1.0);
170 }
171
172 // Calling VertexWeight on a vertex not in the graph will result in
173 // undefined behavior.
174 double VertexWeight(const Vertex& vertex) const {
175 return FindOrDie(vertex_weights_, vertex);
176 }
177
178 // Calling EdgeWeight on a pair of vertices where either one of the
179 // vertices is not present in the graph will result in undefined
180 // behaviour. If there is no edge connecting vertex1 and vertex2,
181 // the edge weight is zero.
182 double EdgeWeight(const Vertex& vertex1, const Vertex& vertex2) const {
183 if (vertex1 < vertex2) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800184 return FindWithDefault(
185 edge_weights_, std::make_pair(vertex1, vertex2), 0.0);
Austin Schuh70cc9552019-01-21 19:46:48 -0800186 } else {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800187 return FindWithDefault(
188 edge_weights_, std::make_pair(vertex2, vertex1), 0.0);
Austin Schuh70cc9552019-01-21 19:46:48 -0800189 }
190 }
191
192 // Calling Neighbors on a vertex not in the graph will result in
193 // undefined behaviour.
194 const std::unordered_set<Vertex>& Neighbors(const Vertex& vertex) const {
195 return FindOrDie(edges_, vertex);
196 }
197
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800198 const std::unordered_set<Vertex>& vertices() const { return vertices_; }
Austin Schuh70cc9552019-01-21 19:46:48 -0800199
200 static double InvalidWeight() {
201 return std::numeric_limits<double>::quiet_NaN();
202 }
203
204 private:
205 std::unordered_set<Vertex> vertices_;
206 std::unordered_map<Vertex, double> vertex_weights_;
207 std::unordered_map<Vertex, std::unordered_set<Vertex>> edges_;
208 std::unordered_map<std::pair<Vertex, Vertex>, double, pair_hash>
209 edge_weights_;
210};
211
212} // namespace internal
213} // namespace ceres
214
215#endif // CERES_INTERNAL_GRAPH_H_