Skip to content

Commit d754633

Browse files
authored
Add files via upload
1 parent 97009c5 commit d754633

File tree

2 files changed

+190
-0
lines changed

2 files changed

+190
-0
lines changed

src/detour.cpp

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
#include <iostream>
2+
#include <queue>
3+
#include <vector>
4+
#include <fstream>
5+
#include <limits>
6+
#include <functional>
7+
#include <string>
8+
#include <Rcpp.h>
9+
10+
11+
12+
13+
using namespace std;
14+
15+
// [[Rcpp::plugins(cpp11)]]
16+
// [[Rcpp::export]]
17+
18+
Rcpp::List Detour(std::vector<int> dep, std::vector<int> arr,std::vector<int> gfrom,std::vector<int> gto,std::vector<double> gw,int NbNodes, double t, std::vector<std::string> dict){
19+
20+
Rcpp::List final(dep.size());
21+
22+
23+
24+
25+
26+
struct comp{
27+
28+
bool operator()(const std::pair<int, double> &a, const std::pair<int, double> &b){
29+
return a.second > b.second;
30+
}
31+
};
32+
33+
//Graphs
34+
35+
int NbEdges=gfrom.size();
36+
37+
std::vector<std::vector<std::pair<int, double> > > G(NbNodes);
38+
std::vector<std::vector<std::pair<int, double> > > Gr(NbNodes);
39+
40+
for (unsigned int i = 0; i < NbEdges; ++i) {
41+
42+
G[gfrom[i]].push_back(std::make_pair(gto[i], gw[i]));
43+
Gr[gto[i]].push_back(std::make_pair(gfrom[i], gw[i]));
44+
45+
46+
}
47+
48+
49+
//Boucle sur chaque trajet
50+
51+
for (unsigned int k=0; k!=dep.size();k++){
52+
if (k % 256){
53+
Rcpp::checkUserInterrupt ();
54+
}
55+
56+
int StartNode=dep[k];
57+
int EndNode=arr[k];
58+
59+
std::vector<double> Distances(NbNodes, std::numeric_limits<double>::max());
60+
std::vector<double> Distances2(NbNodes, std::numeric_limits<double>::max());
61+
62+
63+
Distances[StartNode] = 0.0;
64+
Distances2[EndNode] = 0.0;
65+
66+
67+
std::vector <int> Visited(NbNodes,0);
68+
69+
70+
std::priority_queue<std::pair<int, double>, std::vector<std::pair<int, double> >, comp > Q;
71+
std::priority_queue<std::pair<int, double>, std::vector<std::pair<int, double> >, comp > Qr;
72+
Q.push(std::make_pair(StartNode, 0.0));
73+
Qr.push(std::make_pair(EndNode, 0.0));
74+
Visited[StartNode]+=1;
75+
Visited[EndNode]+=1;
76+
77+
double mu=std::numeric_limits<double>::max();
78+
double def_mu=std::numeric_limits<double>::max();
79+
80+
81+
82+
while (!Q.empty() && !Qr.empty()) {
83+
if (Q.top().second+Qr.top().second >= mu){
84+
def_mu=mu;
85+
}
86+
87+
if (Q.top().second > def_mu + t && Qr.top().second > def_mu+t){
88+
break;
89+
}
90+
91+
92+
if (!Q.empty()){
93+
int v=Q.top().first;
94+
int w=Q.top().second;
95+
Q.pop();
96+
97+
98+
99+
100+
101+
102+
103+
104+
if (w <= Distances[v]) {
105+
for (int i=0; i< G[v].size(); i++){
106+
int v2 = G[v][i].first;
107+
double w2 = G[v][i].second;
108+
109+
110+
if (Distances[v] + w2 < Distances[v2]) {
111+
Distances[v2] = Distances[v] + w2;
112+
113+
114+
Q.push(std::make_pair(v2, Distances[v2]));
115+
Visited[v2]+=1;
116+
117+
}
118+
}
119+
}
120+
121+
122+
if ((Visited[v]>1) && (Distances[v]+Distances2[v]) < mu){
123+
124+
mu=Distances[v]+Distances2[v];
125+
126+
}
127+
}
128+
129+
if (!Qr.empty()){
130+
int vv=Qr.top().first;
131+
int ww=Qr.top().second;
132+
Qr.pop();
133+
134+
135+
Visited[vv]+=1;
136+
137+
138+
if (ww <= Distances2[vv]) {
139+
for (int i=0; i< Gr[vv].size(); i++){
140+
int vv2 = Gr[vv][i].first;
141+
double ww2 = Gr[vv][i].second;
142+
143+
144+
if (Distances2[vv] + ww2 < Distances2[vv2]) {
145+
Distances2[vv2] = Distances2[vv] + ww2;
146+
147+
148+
Qr.push(std::make_pair(vv2, Distances2[vv2]));
149+
Visited[vv2]+=1;
150+
}
151+
}
152+
}
153+
154+
155+
if ((Visited[vv]> 1) && (Distances[vv]+Distances2[vv]) < mu){
156+
157+
mu=Distances[vv]+Distances2[vv];
158+
159+
}
160+
}
161+
162+
}
163+
164+
165+
166+
if (mu >= std::numeric_limits<double>::max()){
167+
continue;
168+
//final[k] = Rcpp::NumericVector::get_na();
169+
}
170+
else {
171+
std::vector<std::string> result;
172+
for (int i=0; i < Distances.size(); ++i){
173+
if (Distances[i]+Distances2[i] < def_mu + t){
174+
result.push_back(dict[i]);
175+
}
176+
}
177+
final[k]=result;
178+
}
179+
180+
181+
182+
183+
}
184+
185+
186+
return final;
187+
188+
}
189+
190+

src/detour.o

32.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)