Distances on Directed Graphs in R
1#include "fund-cycles.h"
2
3//' @noRd
4// [[Rcpp::export]]
5Rcpp::List rcpp_fundamental_cycles (Rcpp::DataFrame graph,
6 Rcpp::DataFrame verts)
7{
8 std::vector <std::string> vert_id = verts ["id"];
9 std::vector <std::string> from = graph ["from"];
10 std::vector <std::string> to = graph ["to"];
11
12 std::unordered_map <std::string, size_t> vert_index;
13 for (size_t i = 0; i < vert_id.size (); i++)
14 vert_index.emplace (vert_id [i], i);
15
16 std::vector <size_t> edge_array (from.size() * 2);
17 for (size_t i = 0; i < from.size (); i++)
18 {
19 edge_array [i * 2] = vert_index.at (from [i]);
20 edge_array [i * 2 + 1] = vert_index.at (to [i]);
21 }
22 graph::Graph <std::string> gr (vert_id, vert_id.size (),
23 edge_array, from.size ());
24 gr.computeFundamentalCycles();
25
26 Rcpp::List ret (gr.m_fundamentalCycles.size ());
27 std::vector<graph::AdjacencyMatrix>::iterator cycle_iter;
28 for (cycle_iter = gr.m_fundamentalCycles.begin();
29 cycle_iter != gr.m_fundamentalCycles.end (); cycle_iter++)
30 {
31 graph::Graph<std::string>::NodePath path =
32 gr.cycleMatrix2nodePath (*cycle_iter);
33 std::vector <std::string> pathi (path.size ());
34 size_t i = 0;
35 for (const std::string* obj: path)
36 {
37 pathi [i++] = *obj;
38 }
39 ret [std::distance (gr.m_fundamentalCycles.begin(), cycle_iter)] = pathi;
40 }
41
42 return ret;
43}