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
|
/*PGR-GNU*****************************************************************
File: pgr_randomSpanningTree.hpp
Copyright (c) 2018-2026 pgRouting developers
Mail: project@pgrouting.org
Copyright (c) 2018 Aditya Pratap Singh
adityapratap.singh28@gmail.com
------
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_PGR_RANDOMSPANNINGTREE_HPP_
#define INCLUDE_SPANNINGTREE_PGR_RANDOMSPANNINGTREE_HPP_
#pragma once
#include <vector>
#include <random>
#include <iostream>
#include <exception>
#include <boost/config.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/random_spanning_tree.hpp>
#include <boost/random/random_number_generator.hpp>
#include <boost/graph/graph_traits.hpp>
#include "cpp_common/path.hpp"
#include "cpp_common/pgr_base_graph.hpp"
#include "cpp_common/interruption.hpp"
template < class G > class Pgr_randomSpanningTree;
// user's functions
// for development
//******************************************
template < class G >
class Pgr_randomSpanningTree {
public:
typedef typename G::V V;
typedef typename G::E E;
std::vector<SpanTree_rt> randomSpanningTree(
G &graph,
int64_t root_vertex);
private:
// Member
std::vector< V > predecessors;
// Function
std::vector< SpanTree_rt >
undirectedGraph(
const G &graph,
int64_t root_vertex) {
std::ostringstream log;
auto v_root(graph.get_V(root_vertex));
std::minstd_rand rng;
/* abort in case of an interruption occurs (e.g. the query is being cancelled) */
CHECK_FOR_INTERRUPTS();
// TODO(aps)
// This function is running in infinite loop
try {
boost::random_spanning_tree(
graph.graph,
rng,
boost::root_vertex(v_root)
.predecessor_map(&predecessors[0])
.weight_map(get(&G::G_T_E::cost, graph.graph)));
} catch (std::exception const &ex) {
log << ex.what();
} catch (...) {
log << "Unknown exception caught";
}
std::vector< SpanTree_rt > result;
return result;
#if 0
std::vector< SpanTree_rt > results;
double totalcost = 0;
SpanTree_rt tmp;
tmp.root_vertex = root_vertex;
tmp.edge = -1;
tmp.cost = 0;
tmp.tree_cost = totalcost;
results.push_back(tmp);
for (size_t j = 0; j < predecessors.size(); j++) {
if (j != v_root) {
SpanTree_rt tmp;
auto start_node = graph.graph[predecessors[j]].id;
auto end_node = graph.graph[j].id; // node
auto v_sn(graph.get_V(start_node));
auto v_en(graph.get_V(end_node));
auto cost = graph[boost::edge(
predecessors[j], j, graph.graph).first].cost;
auto edge_id =
graph.get_edge_id(v_sn, v_en, cost);
totalcost += cost;
tmp.root_vertex = root_vertex;
tmp.edge = edge_id; // edge_id
tmp.cost = cost; // cost
tmp.tree_cost = totalcost; // tree_cost
results.push_back(tmp);
}
}
return results;
}
#endif
};
template < class G >
std::vector<SpanTree_rt>
Pgr_randomSpanningTree< G >::randomSpanningTree(
G &graph,
int64_t root_vertex ) {
predecessors.clear();
// TODO(aps)
// Currently only running for undirected graph
return undirectedGraph(
graph,
root_vertex);
}
#endif // INCLUDE_SPANNINGTREE_PGR_RANDOMSPANNINGTREE_HPP_
|