1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
/*PGR-GNU*****************************************************************
File: mst.hpp
Copyright (c) 2018-2026 pgRouting developers
Mail: project@pgrouting.org
Copyright (c) 2018 Vicky Vergara
mail: vicky at georepublic dot de
------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/
#ifndef INCLUDE_SPANNINGTREE_MST_HPP_
#define INCLUDE_SPANNINGTREE_MST_HPP_
#pragma once
#include <set>
#include <utility>
#include <string>
#include <vector>
#include <cstdint>
#include <visitors/dfs_visitor_with_root.hpp>
#include <visitors/edges_order_bfs_visitor.hpp>
#include <visitors/edges_order_dfs_visitor.hpp>
#include <boost/graph/connected_components.hpp>
#include <boost/graph/filtered_graph.hpp>
#include "cpp_common/base_graph.hpp"
#include "cpp_common/interruption.hpp"
#include "spanningTree/details.hpp"
namespace pgrouting {
namespace functions {
template <class G>
class Pgr_mst {
public:
virtual ~Pgr_mst() = default;
protected:
typedef typename G::B_G B_G;
typedef typename G::V V;
typedef typename G::E E;
typedef typename G::E_i E_i;
protected:
virtual void generate_mst(const G &graph) = 0;
void clear() {
this->m_spanning_tree.clear();
this->m_components.clear();
this->m_tree_id.clear();
}
std::vector<MST_rt>
no_order(const G &graph) {
return no_ordering(graph);
}
std::vector<MST_rt>
dfs_order(const G &graph) {
return dfs_ordering(graph);
}
std::vector<MST_rt>
bfs_order(const G &graph) {
return bfs_ordering(graph);
}
std::vector<MST_rt> mst(const G &graph) {
m_suffix = "";
m_get_component = false;
m_distance = -1;
m_max_depth = -1;
m_roots.clear();
no_neg_costs(graph);
this->generate_mst(graph);
return no_order(graph);
}
std::vector<MST_rt> mstBFS(
const G &graph,
std::vector<int64_t> roots,
int64_t max_depth) {
m_suffix = "BFS";
m_get_component = true;
m_distance = -1;
m_max_depth = max_depth;
m_roots = details::clean_vids(roots);
this->generate_mst(graph);
return bfs_order(graph);
}
std::vector<MST_rt> mstDFS(
const G &graph,
std::vector<int64_t> roots,
int64_t max_depth) {
m_suffix = "DFS";
m_get_component = false;
m_distance = -1;
m_max_depth = max_depth;
m_roots = details::clean_vids(roots);
this->generate_mst(graph);
return dfs_order(graph);
}
std::vector<MST_rt> mstDD(
const G &graph,
std::vector<int64_t> roots,
double distance) {
m_suffix = "DD";
m_get_component = false;
m_distance = distance;
m_max_depth = -1;
m_roots = details::clean_vids(roots);
this->generate_mst(graph);
return dfs_order(graph);
}
protected:
std::vector<int64_t> m_roots;
bool m_get_component;
int64_t m_max_depth;
double m_distance;
struct InSpanning {
std::set<E> edges;
bool operator()(E e) const { return edges.count(e); }
void clear() { edges.clear(); }
} m_spanning_tree;
/** m_components[v]:
* - is empty (when m_get_component = 0)
* - has the component number of vertex v (when m_get_component != 0)
*/
std::vector<size_t> m_components;
/**
* Stores which function is being executed.
*
* TODO change to enum
*/
std::string m_suffix;
/** m_tree_id[v]:
* - is empty (when m_get_component = 0)
* - has the component number of vertex v (when m_get_component = 1)
* - has the min vertex id that belongs to the component (when m_get_component = 2)
*/
std::vector<int64_t> m_tree_id;
private:
template <typename T>
std::vector<MST_rt> get_results(
T order,
int64_t p_root,
const G &graph) {
std::vector<MST_rt> results;
std::vector<double> agg_cost(graph.num_vertices(), 0);
std::vector<int64_t> depth(graph.num_vertices(), 0);
int64_t root(p_root);
for (const auto edge : order) {
auto u = graph.source(edge);
auto v = graph.target(edge);
if (depth[u] == 0 && depth[v] != 0) {
std::swap(u, v);
}
auto component = m_get_component? m_tree_id[m_components[u]] : 0;
if (m_suffix != "" && depth[u] == 0 && depth[v] == 0) {
if (!m_roots.empty() && graph[u].id != root) std::swap(u, v);
if (m_roots.empty() && graph[u].id != component) std::swap(u, v);
if (!p_root && graph[u].id > graph[v].id) std::swap(u, v);
root = p_root? p_root: graph[u].id;
depth[u] = -1;
results.push_back({
root,
0,
graph[u].id,
graph[u].id,
-1,
0.0,
0.0 });
}
agg_cost[v] = agg_cost[u] + graph[edge].cost;
depth[v] = depth[u] == -1? 1 : depth[u] + 1;
if ((m_suffix == "")
|| ((m_suffix == "BFS") && (m_max_depth >= depth[v]))
|| ((m_suffix == "DFS") && m_max_depth >= depth[v])
|| ((m_suffix == "DD") && m_distance >= agg_cost[v])) {
results.push_back({
root,
m_suffix != ""? depth[v] : 0,
graph[u].id,
graph[v].id,
graph[edge].id,
graph[edge].cost,
m_suffix != ""? agg_cost[v] : 0.0
});
}
}
return results;
}
void calculate_component(const G &graph) {
if (!m_get_component) return;
m_components.resize(num_vertices(graph.graph));
/*
* Calculate connected components
*
* Number of components of graph: num_comps components
*/
auto num_comps = boost::connected_components(
graph.graph,
&m_components[0]);
m_tree_id.resize(num_comps, 0);
for (const auto v : boost::make_iterator_range(vertices(graph.graph))) {
m_tree_id[m_components[v]] =
(m_tree_id[m_components[v]] == 0
|| m_tree_id[m_components[v]] >= graph[v].id) ?
graph[v].id : m_tree_id[m_components[v]];
}
}
std::vector<MST_rt>
no_ordering(const G &graph) {
return get_results(m_spanning_tree.edges, 0, graph);
}
std::vector<MST_rt>
dfs_forest(const G &graph) {
boost::filtered_graph<B_G, InSpanning, boost::keep_all>
mstGraph(graph.graph, m_spanning_tree, {});
std::vector<E> visited_order;
using dfs_visitor = visitors::Edges_order_dfs_visitor<E>;
/* abort in case of an interruption occurs (e.g. the query is being cancelled) */
CHECK_FOR_INTERRUPTS();
try {
boost::depth_first_search(mstGraph, visitor(dfs_visitor(visited_order)));
} catch (boost::exception const& ex) {
(void)ex;
throw;
} catch (std::exception &e) {
(void)e;
throw;
} catch (...) {
throw;
}
return get_results(visited_order, 0, graph);
}
std::vector<MST_rt>
dfs_ordering(const G &graph) {
boost::filtered_graph<B_G, InSpanning, boost::keep_all>
mstGraph(graph.graph, m_spanning_tree, {});
if (m_roots.empty()) {
return dfs_forest(graph);
} else {
std::vector<MST_rt> results;
for (const auto root : m_roots) {
using dfs_visitor = visitors::Dfs_visitor_with_root<V, E>;
if (graph.has_vertex(root)) {
std::vector<E> visited_order;
CHECK_FOR_INTERRUPTS();
try {
boost::depth_first_search(
mstGraph,
visitor(dfs_visitor(graph.get_V(root), visited_order))
.root_vertex(graph.get_V(root)));
} catch(found_goals &) {
{}
} catch (boost::exception const& ex) {
(void)ex;
throw;
} catch (std::exception &e) {
(void)e;
throw;
} catch (...) {
throw;
}
auto result = get_results(visited_order, root, graph);
results.insert(results.end(), result.begin(), result.end());
} else {
results.push_back({root, 0, root, root, -1, 0.0, 0.0});
}
}
return results;
}
}
std::vector<MST_rt>
bfs_ordering(const G &graph) {
std::vector<MST_rt> results;
/*
* order by bfs
*/
boost::filtered_graph<B_G, InSpanning, boost::keep_all>
mst(graph.graph, m_spanning_tree, {});
calculate_component(graph);
std::vector<int64_t> roots;
if (!m_roots.empty()) {
roots = m_roots;
} else {
roots = m_tree_id;
}
using bfs_visitor = visitors::Edges_order_bfs_visitor<E>;
for (auto root : roots) {
std::vector<E> visited_order;
if (graph.has_vertex(root)) {
boost::breadth_first_search(mst,
graph.get_V(root),
visitor(bfs_visitor(visited_order)));
auto tree_results = get_results(visited_order, root, graph);
results.insert(results.end(), tree_results.begin(), tree_results.end());
} else {
results.push_back({root, 0, root, root, -1, 0.0, 0.0});
}
/* abort in case of an interruption occurs (e.g. the query is being cancelled) */
CHECK_FOR_INTERRUPTS();
}
return results;
}
bool no_neg_costs(const G &graph) {
E_i ei, ei_end;
for (boost::tie(ei, ei_end) = edges(graph.graph); ei != ei_end; ++ei) {
pgassert(graph[*ei].cost > 0);
}
return true;
}
};
} // namespace functions
} // namespace pgrouting
#endif // INCLUDE_SPANNINGTREE_MST_HPP_
|