42 #ifndef OMPL_DATASTRUCTURES_DYNAMICSSSP_H 43 #define OMPL_DATASTRUCTURES_DYNAMICSSSP_H 50 #include <boost/graph/graph_traits.hpp> 51 #include <boost/graph/adjacency_list.hpp> 52 #include <unordered_set> 61 graph_ =
new Graph(0);
68 void addVertex(std::size_t
id)
70 distance_.push_back((
id == 0) ? 0 : std::numeric_limits<double>::infinity());
71 parent_.push_back(NO_ID);
72 boost::add_vertex(
id, *graph_);
78 void addEdge(std::size_t v, std::size_t w,
double weight,
79 bool collectVertices, std::list<std::size_t>& affectedVertices)
82 WeightProperty edge_property(weight);
83 boost::add_edge(v, w, edge_property, *graph_);
86 assert ( (distance_[v] == std::numeric_limits<double>::infinity()) ||
87 (distance_[w] == std::numeric_limits<double>::infinity()) ||
88 (distance_[w] + weight != distance_[w]) );
90 std::vector<double> cost( boost::num_vertices(*graph_),
91 std::numeric_limits<double>::infinity());
93 IsLessThan isLessThan(cost);
94 Queue queue(isLessThan);
96 if (distance_[v] + weight < distance_[w])
98 distance_[w] = distance_[v] + weight;
105 WeightMap weights = boost::get(boost::edge_weight_t(), *graph_);
106 while (!queue.empty())
109 std::size_t u = *(queue.begin());
110 queue.erase(queue.begin());
113 affectedVertices.push_back(u);
115 boost::out_edges(u, *graph_);
118 boost::graph_traits<Graph>::out_edge_iterator ei, ei_end;
119 for (boost::tie(ei, ei_end) = boost::out_edges(u, *graph_); ei != ei_end; ++ei)
121 std::size_t x = boost::target(*ei, *graph_);
122 double edgeWeight = boost::get(weights, *ei);
124 if (distance_[u] + edgeWeight < distance_[x])
126 distance_[x] = distance_[u] + edgeWeight;
130 QueueIter qIter = queue.find(x);
131 if (qIter != queue.end() )
134 cost[x] = distance_[x] - distance_[v];
143 void removeEdge(std::size_t v, std::size_t w,
144 bool collectVertices, std::list<std::size_t>& affectedVertices)
147 boost::remove_edge(v, w, *graph_);
152 std::list<std::size_t> workSet;
153 IntSet affectedVerticesSet;
154 workSet.push_back(w);
156 while (!workSet.empty())
159 std::size_t u = workSet.front();
162 affectedVerticesSet.insert(u);
164 boost::graph_traits<Graph>::out_edge_iterator ei, ei_end;
165 for (boost::tie(ei, ei_end) = boost::out_edges(u, *graph_); ei != ei_end; ++ei)
167 std::size_t x = boost::target(*ei, *graph_);
169 workSet.push_back(x);
173 WeightMap weights = boost::get(boost::edge_weight_t(), *graph_);
176 IsLessThan isLessThan(distance_);
177 Queue queue(isLessThan);
178 for (IntSetIter set_iter = affectedVerticesSet.begin(); set_iter!= affectedVerticesSet.end(); ++set_iter)
180 std::size_t a = *set_iter;
181 distance_[a] = std::numeric_limits<double>::infinity();
185 boost::graph_traits<Graph>::in_edge_iterator ei, ei_end;
186 for (boost::tie(ei, ei_end) = boost::in_edges(a, *graph_); ei != ei_end; ++ei)
188 std::size_t b = boost::source(*ei, *graph_);
189 if (affectedVerticesSet.find(b) == affectedVerticesSet.end())
191 double edgeWeight = boost::get(weights, *ei);
193 if (distance_[b] + edgeWeight < distance_[a])
195 distance_[a] = distance_[b] + edgeWeight;
200 if (distance_[a] != std::numeric_limits<double>::infinity())
204 while(!queue.empty())
207 std::size_t a = *queue.begin();
208 queue.erase(queue.begin());
211 affectedVertices.push_back(a);
214 boost::graph_traits<Graph>::out_edge_iterator ei, ei_end;
215 for (boost::tie(ei, ei_end) = boost::out_edges(a, *graph_); ei != ei_end; ++ei)
217 int c = boost::target(*ei, *graph_);
218 double edgeWeight = boost::get(weights, *ei);
220 if (distance_[a] + edgeWeight < distance_[c])
222 distance_[c] = distance_[a] + edgeWeight;
226 QueueIter qIter = queue.find(c);
227 if (qIter != queue.end() )
238 double getShortestPathCost(std::size_t u)
const 240 return this->distance_[u];
243 std::size_t getShortestPathParent(std::size_t u)
const 248 typedef boost::property<boost::edge_weight_t, double> WeightProperty;
249 typedef boost::adjacency_list<boost::vecS,
251 boost::bidirectionalS,
255 typedef boost::property_map<Graph, boost::edge_weight_t>::type WeightMap;
257 static const int NO_ID = -1;
262 IsLessThan(std::vector<double>& cost)
267 bool operator()(std::size_t id1, std::size_t id2)
const 269 return (cost_[id1] < cost_[id2]);
272 std::vector<double>& cost_;
275 typedef std::set<std::size_t, IsLessThan> Queue;
276 typedef Queue::iterator QueueIter;
277 typedef std::unordered_set<std::size_t> IntSet;
278 typedef IntSet::iterator IntSetIter;
282 std::vector<double> distance_;
284 std::vector<std::size_t> parent_;
288 #endif //OMPL_DATASTRUCTURES_DYNAMICSSSP_H Main namespace. Contains everything in this library.