-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdijkstra.cc
264 lines (255 loc) · 10.9 KB
/
dijkstra.cc
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
#include "global.h"
#include "proto.h"
#include "pruned_landmark_labeling.h"
using namespace std;
//variant of dijkstra algorithm
//return the ids in the path and the lightest weight of path. The order of ids in the path might not be correct because of set. Will change the data structure if needed.
//variant of Dijkstra's approach with exact distance oracle.
QueryResult dijkstra(const graph_t& g, PrunedLandmarkLabeling<> &pll, Query query, bool oracle){
int maxDepth = query.pattern.size()-1;
float minWgt = MAX_WEIGHT;
QueryResult qResult;
if( (g.typeMap[query.src]!=query.pattern[0]) || (g.typeMap[query.tgt]!=query.pattern[maxDepth]) ){
cout << query.src << " to " << query.tgt << endl;
cout << g.typeMap[query.src] << " and " << g.typeMap[query.tgt] << endl;
cout<< "src or tgt node does not follow pattern!" << endl;
return qResult;
}
std::priority_queue<Path, std::vector<Path>, comparator> frontier;
vector<int> tmpPath;
tmpPath.push_back(query.src);
frontier.push(createPath(0, tmpPath));//frontier is the priority queue.
int mem = 1, total=1;
while(!frontier.empty()){
mem = max(mem, (int)frontier.size());
Path curNode;
curNode = frontier.top();
frontier.pop();
int curId = curNode.nodeIds.back();
int depth = curNode.nodeIds.size()-1;//start from 0.
vector<int> path = curNode.nodeIds;
if(depth==maxDepth){//reach the end of pattern.
if(curId == query.tgt && curNode.wgt<MAX_WEIGHT){
if(qResult.paths.size()<TOP_K)
qResult.paths.push_back(createPath(curNode.wgt, path));
if(qResult.paths.size()==TOP_K)
break;
}
continue;
}
//expanding the neighbors of current node.
for(int i=0; i<g.degree[curId]; i++){
int neigh = g.neighbors[g.nodes[curId]+i];//neighbor id.
float edgwgt = calcWgt(g.wgts[g.nodes[curId]+i], query.time);
//if neighbor is not in the path and follow the pattern.
if(g.typeMap[neigh] == query.pattern[depth+1] && find(path.begin(), path.end(), neigh) == path.end() && edgwgt+curNode.wgt<MAX_WEIGHT){
if(oracle == WITH_ORACLE){
int tree_dist = pll.QueryDistance(query.tgt, neigh);//use exact distance oracle.
int realDistInPattern = query.pattern.size()-depth-2;
if(tree_dist > realDistInPattern)
continue;
}
vector<int> newPath = path;
newPath.push_back(neigh);
frontier.push(createPath(edgwgt+curNode.wgt,newPath));
total += 1;
}
}
}
qResult.mem = mem;
qResult.totalPaths = total;
return qResult;
}
QueryResult dijkstra_with_Prophet(const graph_t& g, Query query){
assert(query.pattern.size()>2);//otherwise the problem becomes trivial.
int maxDepth = query.pattern.size()-1;
float minWgt = MAX_WEIGHT;
QueryResult qResult;
if( (g.typeMap[query.src]!=query.pattern[0]) || (g.typeMap[query.tgt]!=query.pattern[maxDepth]) ){
cout << query.src << " to " << query.tgt << endl;
cout << g.typeMap[query.src] << " and " << g.typeMap[query.tgt] << endl;
cout<< "src or tgt node does not follow pattern!" << endl;
return qResult;
}
//layers stores the legitimate nodes on each level. Each layer is a set.
double tp;
vector<unordered_set<int> > layers = create_Prophet(g, query, tp);
std::priority_queue<Path, std::vector<Path>, comparator> frontier;
vector<int> tmpPath;
tmpPath.push_back(query.src);
frontier.push(createPath(0, tmpPath));//frontier is the priority queue.
int mem = 1, total=1;
while(!frontier.empty()){
mem = max(mem, (int)frontier.size());
Path curNode;
curNode = frontier.top();
frontier.pop();
int curId = curNode.nodeIds.back();
int depth = curNode.nodeIds.size()-1;//start from 0.
vector<int> path = curNode.nodeIds;
if(depth==maxDepth){//reach the end of pattern.
if(curId == query.tgt && curNode.wgt<MAX_WEIGHT){
if(qResult.paths.size()<TOP_K)
qResult.paths.push_back(createPath(curNode.wgt, path));
if(qResult.paths.size()==TOP_K)
break;
}
continue;
}
//expanding the neighbors of current node.
for(int i=0; i<g.degree[curId]; i++){
int neigh = g.neighbors[g.nodes[curId]+i];//neighbor id.
float edgwgt = calcWgt(g.wgts[g.nodes[curId]+i], query.time);
//if neighbor is not in the path and follow the pattern.
if( layers[depth+1].count(neigh)>0 && find(path.begin(), path.end(), neigh) == path.end() && edgwgt+curNode.wgt<MAX_WEIGHT){
vector<int> newPath = path;
newPath.push_back(neigh);
frontier.push(createPath(edgwgt+curNode.wgt, newPath));
total += 1;
// layers[depth+1].at(neigh) = -layers[depth+1].at(neigh); //Note: important. Avoid repeated visit to the node in the same layer.
//a few more words on this. It seems we should not do this. Because the possibility of loops.
}
}
}
qResult.mem = mem;
qResult.totalPaths = total;
return qResult;
}
//The implementation of this one is not completed.
//But this is not going to work properly since it cannot deal with the top-K case.
QueryResult bidir_Dijkstra(const graph_t& g, PrunedLandmarkLabeling<> &pll, Query query, bool oracle){
int maxDepth = query.pattern.size()-1;
float minWgt = MAX_WEIGHT;
QueryResult qResult;
if( (g.typeMap[query.src]!=query.pattern[0]) || (g.typeMap[query.tgt]!=query.pattern[maxDepth]) ){
cout << query.src << " to " << query.tgt << endl;
cout << g.typeMap[query.src] << " and " << g.typeMap[query.tgt] << endl;
cout<< "src or tgt node does not follow pattern!" << endl;
return qResult;
}
std::priority_queue<Path, std::vector<Path>, comparator> forward;
std::priority_queue<Path, std::vector<Path>, comparator> backward;
unordered_map<int, Path> forwardVisited;
unordered_map<int, Path> backwardVisited;
priority_queue<Path, std::vector<Path>, comparator2> candidates;
vector<int> tmpPath;
tmpPath.push_back(query.src);
forward.push(createPath(0, tmpPath));//frontier is the priority queue.
tmpPath.pop_back();
tmpPath.push_back(query.tgt);
backward.push(createPath(0, tmpPath));
int mem = 1, total=2, flag = 0;
while(!forward.empty() || !backward.empty()){
mem = max(mem, (int)(forward.size()+backward.size()));
flag = 1-flag;
Path curNode;
if(flag == 1){
curNode = forward.top();
forward.pop();
int curId = curNode.nodeIds.back();
int depth = curNode.nodeIds.size()-1;//start from 0.
vector<int> path = curNode.nodeIds;
forwardVisited[curId] = createPath(curNode.wgt, path);
if(depth==maxDepth){//reach the end of pattern.
continue;
}
if(backwardVisited.count(curId)>0){
int newWgt= curNode.wgt + backwardVisited[curId].wgt;
path.pop_back();
for(vector<int>::reverse_iterator rit=backwardVisited[curId].nodeIds.rbegin(); rit!=backwardVisited[curId].nodeIds.rend(); rit++){
path.push_back(*rit);
}
if(path.size()==query.pattern.size())
candidates.push(createPath(newWgt,path));
break;}
//expanding the neighbors of current node.
for(int i=0; i<g.degree[curId]; i++){
int neigh = g.neighbors[g.nodes[curId]+i];//neighbor id.
float edgwgt = calcWgt(g.wgts[g.nodes[curId]+i], query.time);
//if neighbor is not in the path and follow the pattern.
if(g.typeMap[neigh] == query.pattern[depth+1] && forwardVisited.count(neigh)==0
&& edgwgt+curNode.wgt<MAX_WEIGHT){
if(oracle == WITH_ORACLE){
int tree_dist = pll.QueryDistance(query.tgt, neigh);//use exact distance oracle.
int realDistInPattern = query.pattern.size()-depth-2;
if(tree_dist > realDistInPattern)
continue;
}
vector<int> newPath = path;
newPath.push_back(neigh);
forward.push(createPath(edgwgt+curNode.wgt,newPath));
total += 1;
}
}
}else{
curNode = backward.top();
backward.pop();
int curId = curNode.nodeIds.back();
int depth = curNode.nodeIds.size()-1;//start from 0.
vector<int> path = curNode.nodeIds;
backwardVisited[curId] = createPath(curNode.wgt, path);
if(depth==maxDepth){//reach the end of pattern.
continue;
}
if(forwardVisited.count(curId)>0){
int newWgt = curNode.wgt + forwardVisited[curId].wgt;
vector<int> candidatePath = forwardVisited[curId].nodeIds;
candidatePath.pop_back();
for(vector<int>::reverse_iterator rit=path.rbegin(); rit!=path.rend(); rit++){
candidatePath.push_back(*rit);
}
if(candidatePath.size() == query.pattern.size())
candidates.push(createPath(newWgt, candidatePath));
break;}
//expanding the neighbors of current node.
for(int i=0; i<g.degree[curId]; i++){
int neigh = g.neighbors[g.nodes[curId]+i];//neighbor id.
float edgwgt = calcWgt(g.wgts[g.nodes[curId]+i], query.time);
//if neighbor is not in the path and follow the pattern.
if(g.typeMap[neigh] == query.pattern[depth+1] && backwardVisited.count(neigh)==0
&& edgwgt+curNode.wgt<MAX_WEIGHT){
if(oracle == WITH_ORACLE){
int tree_dist = pll.QueryDistance(query.src, neigh);//use exact distance oracle.
int realDistInPattern = query.pattern.size()-depth-2;
if(tree_dist > realDistInPattern)
continue;
}
vector<int> newPath = path;
newPath.push_back(neigh);
backward.push(createPath(edgwgt+curNode.wgt,newPath));
total += 1;
}
}
}
}
if(!forward.empty() || !backward.empty()){
for(auto it=forwardVisited.begin(); it!=forwardVisited.end(); it++){
int curId = it->first; Path tmpPath = it->second; float wgt = tmpPath.wgt; int len = tmpPath.nodeIds.size();
for(int i=0; wgt<minWgt && i<g.degree[curId]; i++){
int neigh = g.neighbors[g.nodes[curId]+i];
float edgeWgt = calcWgt(g.wgts[g.nodes[curId]+i], query.time);
if(backwardVisited.count(neigh)>0 && wgt+edgeWgt+backwardVisited[neigh].wgt<minWgt &&
len+backwardVisited.at(neigh).nodeIds.size()==query.pattern.size()){
// cout << len <<" "<<backwardVisited.at(neigh).nodeIds.size()<<" == "<<query.pattern.size() << " @@ "<<neigh<<" ## ";
vector<int> candidatePath = tmpPath.nodeIds;
int newWgt = wgt+edgeWgt+backwardVisited[neigh].wgt;
if(candidates.size()==TOP_K)
candidates.pop();
for(vector<int>::reverse_iterator rit=backwardVisited[neigh].nodeIds.rbegin(); rit!=backwardVisited[neigh].nodeIds.rend(); rit++){
// cout << " " << *rit;
candidatePath.push_back(*rit);
}
// cout << endl;
candidates.push(createPath(newWgt,candidatePath));
if(candidates.size()==TOP_K)
minWgt = candidates.top().wgt;
total += 1;
}
}
}
}
qResult.paths = pq2vec(candidates);
qResult.mem = mem;
qResult.totalPaths = total;
return qResult;
}