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