DynamicSSSP.h
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2015, Tel Aviv University
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Tel Aviv University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: Oren Salzman */
36 /* Implementation based on
37 G. Ramalingam and T. W. Reps, On the computational complexity of
38 dynamic graph problems, Theor. Comput. Sci., vol. 158, no. 1&2, pp.
39 233-277, 1996.
40 */
41 
42 #ifndef OMPL_DATASTRUCTURES_DYNAMICSSSP_H
43 #define OMPL_DATASTRUCTURES_DYNAMICSSSP_H
44 
45 #include <list>
46 #include <set>
47 #include <vector>
48 #include <limits>
49 
50 #include <boost/graph/graph_traits.hpp>
51 #include <boost/graph/adjacency_list.hpp>
52 #include <unordered_set>
53 
54 namespace ompl
55 {
57  {
58  public:
59  DynamicSSSP()
60  {
61  graph_ = new Graph(0);
62  }
63  ~DynamicSSSP()
64  {
65  delete graph_;
66  }
67 
68  void addVertex(std::size_t id)
69  {
70  distance_.push_back((id == 0) ? 0 : std::numeric_limits<double>::infinity());
71  parent_.push_back(NO_ID);
72  boost::add_vertex(id, *graph_);
73  }
74 
75  // we assume that no two paths have the same cost,
76  // this asssumption is valid when the nodes have some randomeness to them
77  void addEdge(std::size_t v, std::size_t w, double weight, bool collectVertices,
78  std::list<std::size_t> &affectedVertices)
79  {
80  // first, add edge to graph
81  WeightProperty edge_property(weight);
82  boost::add_edge(v, w, edge_property, *graph_);
83 
84  // now, update distance_
85  assert((distance_[v] == std::numeric_limits<double>::infinity()) ||
86  (distance_[w] == std::numeric_limits<double>::infinity()) ||
87  (distance_[w] + weight != distance_[w]));
88 
89  std::vector<double> cost(boost::num_vertices(*graph_),
90  std::numeric_limits<double>::infinity()); // initialize to n values of cost oo
91 
92  IsLessThan isLessThan(cost);
93  Queue queue(isLessThan);
94 
95  if (distance_[v] + weight < distance_[w])
96  {
97  distance_[w] = distance_[v] + weight;
98  parent_[w] = v;
99 
100  cost[w] = 0;
101  queue.insert(w);
102  }
103 
104  WeightMap weights = boost::get(boost::edge_weight_t(), *graph_);
105  while (!queue.empty())
106  {
107  // pop head of queue
108  std::size_t u = *(queue.begin());
109  queue.erase(queue.begin());
110 
111  if (collectVertices)
112  affectedVertices.push_back(u);
113 
114  boost::out_edges(u, *graph_);
115 
116  // for every outgoing edge, see if we can improve its cost
117  boost::graph_traits<Graph>::out_edge_iterator ei, ei_end;
118  for (boost::tie(ei, ei_end) = boost::out_edges(u, *graph_); ei != ei_end; ++ei)
119  {
120  std::size_t x = boost::target(*ei, *graph_);
121  double edgeWeight = boost::get(weights, *ei);
122 
123  if (distance_[u] + edgeWeight < distance_[x])
124  {
125  distance_[x] = distance_[u] + edgeWeight;
126  parent_[x] = u;
127 
128  // insert to queue
129  auto qIter = queue.find(x);
130  if (qIter != queue.end())
131  queue.erase(qIter);
132 
133  cost[x] = distance_[x] - distance_[v];
134  queue.insert(x);
135  }
136  }
137  }
138  }
139 
140  void removeEdge(std::size_t v, std::size_t w, bool collectVertices, std::list<std::size_t> &affectedVertices)
141  {
142  // first, remove edge from graph
143  boost::remove_edge(v, w, *graph_);
144  if (parent_[w] != v)
145  return;
146 
147  // Phase 1: Identify the affected vertices and remove the affected edges from SP(G)
148  std::list<std::size_t> workSet;
149  IntSet affectedVerticesSet;
150  workSet.push_back(w);
151 
152  while (!workSet.empty())
153  {
154  // S elect and remove a vertex u from WorkSet
155  std::size_t u = workSet.front();
156  workSet.pop_front();
157 
158  affectedVerticesSet.insert(u);
159 
160  boost::graph_traits<Graph>::out_edge_iterator ei, ei_end;
161  for (boost::tie(ei, ei_end) = boost::out_edges(u, *graph_); ei != ei_end; ++ei)
162  {
163  std::size_t x = boost::target(*ei, *graph_);
164  if (parent_[x] == u)
165  workSet.push_back(x);
166  }
167  }
168 
169  WeightMap weights = boost::get(boost::edge_weight_t(), *graph_);
170 
171  // Phase 2: Determine new distances from affected vertices to source(G) and update SP(G).
172  IsLessThan isLessThan(distance_);
173  Queue queue(isLessThan);
174  for (auto set_iter = affectedVerticesSet.begin(); set_iter != affectedVerticesSet.end(); ++set_iter)
175  {
176  std::size_t a = *set_iter;
177  distance_[a] = std::numeric_limits<double>::infinity();
178 
179  // go over all incoming neighbors which are NOT affected vertices
180  // get the best such neighbor
181  boost::graph_traits<Graph>::in_edge_iterator ei, ei_end;
182  for (boost::tie(ei, ei_end) = boost::in_edges(a, *graph_); ei != ei_end; ++ei)
183  {
184  std::size_t b = boost::source(*ei, *graph_);
185  if (affectedVerticesSet.find(b) == affectedVerticesSet.end())
186  {
187  double edgeWeight = boost::get(weights, *ei);
188 
189  if (distance_[b] + edgeWeight < distance_[a])
190  {
191  distance_[a] = distance_[b] + edgeWeight;
192  parent_[a] = b;
193  }
194  }
195  }
196  if (distance_[a] != std::numeric_limits<double>::infinity())
197  queue.insert(a);
198  }
199 
200  while (!queue.empty())
201  {
202  // pop head of queue
203  std::size_t a = *queue.begin();
204  queue.erase(queue.begin());
205 
206  if (collectVertices)
207  affectedVertices.push_back(a);
208 
209  // for every outgoing edge, see if we can improve its cost
210  boost::graph_traits<Graph>::out_edge_iterator ei, ei_end;
211  for (boost::tie(ei, ei_end) = boost::out_edges(a, *graph_); ei != ei_end; ++ei)
212  {
213  int c = boost::target(*ei, *graph_);
214  double edgeWeight = boost::get(weights, *ei);
215 
216  if (distance_[a] + edgeWeight < distance_[c])
217  {
218  distance_[c] = distance_[a] + edgeWeight;
219  parent_[c] = a;
220 
221  // insert to queue
222  auto qIter = queue.find(c);
223  if (qIter != queue.end())
224  queue.erase(qIter);
225 
226  queue.insert(c);
227  }
228  }
229  }
230  }
231 
232  double getShortestPathCost(std::size_t u) const
233  {
234  return this->distance_[u];
235  }
236 
237  std::size_t getShortestPathParent(std::size_t u) const
238  {
239  return parent_[u];
240  }
241 
242  private:
243  using WeightProperty = boost::property<boost::edge_weight_t, double>;
244  using Graph = boost::adjacency_list<boost::vecS, // container type for the edge list
245  boost::vecS, // container type for the vertex list
246  boost::bidirectionalS, // directedS / undirectedS / bidirectionalS
247  std::size_t, // vertex properties
248  WeightProperty>; // edge properties
249  using WeightMap = boost::property_map<Graph, boost::edge_weight_t>::type;
250 
251  static const int NO_ID = -1;
252 
253  class IsLessThan
254  {
255  public:
256  IsLessThan(std::vector<double> &cost) : cost_(cost)
257  {
258  }
259 
260  bool operator()(std::size_t id1, std::size_t id2) const
261  {
262  return (cost_[id1] < cost_[id2]);
263  }
264 
265  private:
266  std::vector<double> &cost_;
267  }; // IsLessThan
268 
269  using Queue = std::set<std::size_t, IsLessThan>;
270  using QueueIter = Queue::iterator;
271  using IntSet = std::unordered_set<std::size_t>;
272  using IntSetIter = IntSet::iterator;
273 
274  Graph *graph_;
276  std::vector<double> distance_;
278  std::vector<std::size_t> parent_;
279  }; // DynamicSSSP
280 }
281 
282 #endif // OMPL_DATASTRUCTURES_DYNAMICSSSP_H
Main namespace. Contains everything in this library.
Definition: AppBase.h:21