-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglobal.h
295 lines (228 loc) · 7.69 KB
/
global.h
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
#ifndef GLOBAL_HEADER
#define GLOBAL_HEADER
#include <unordered_set>
#include "math.h"
#include <iostream>
#include <fstream>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unordered_map>
#include <stack>
#include <queue>
#include <ios>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <sys/time.h>
#include <stdbool.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
#include <time.h>
#include <tuple>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <unistd.h>
#include <map>
#define MAX_WEIGHT 100000000
#define TOP_K 10
#define WITH_ORACLE true
#define WITHOUT_ORACLE false
#define SAFE_LIFT 1000000
typedef struct Query{
int src;
int tgt;
float time;
std::vector<int> pattern; //only types of nodes here
}Query;
typedef struct GeneralizedQuery{ //generalized, reusable query allowing src and tgt to be sets
std::unordered_map<int, float> srcs;
std::unordered_map<int, float> tgts;
int pos_junction; //used for the next iteration indexing. which level is junction node -------------specified by the post-order traverse
float time;
std::vector<int> pattern; //only types of nodes here
std::vector<int> nodes;
}GeneralizedQuery;
typedef std::pair<int, int> Edge;
typedef struct Shape{
int shape_index; //just a name index of what shape it is for convenience
int seed_index; //which node is seed
int adj[100][100];
int nodenum;
}Shape;
//the first nodenum row and column in adj is the adjacency matrix of this shape
typedef struct Non_bi_tree{
std::unordered_map<int, std::vector<int>> map2chr;
std::unordered_map<int, int> map2parent;
std::unordered_map<int, int> map2pattern;
float time;
}Non_bi_tree;
typedef struct Query_tree{
//given a tree query input. At this point, we assume all queries are trees
//we have already decomposed the tree into post-order list of nodes
std::unordered_map<int, int> map2leftcdr;
std::unordered_map<int, int> map2rightcdr;
std::unordered_map<int, int> map2parent;
std::unordered_map<int, int> map2patthern;
std::vector<int> nodes_ordered; //unknown set as 0.
std::vector<int> terminals_index; //the position of n terminals in the ordered nodes
std::vector<int> junction_index; //the position of (n-1) junction nodes
std::vector<int> junctions;
std::vector<int> terminals;
std::vector<int> patterns;//the type of nodes
float time;
}Query_tree;
class Tree_query{
public: Query_tree querytree;
};
typedef struct Query_tree_fixed{
//given a tree query input. At this point, we assume all queries are trees
//we have already decomposed the tree into post-order list of nodes
std::unordered_map<int, int> map2leftcdr;
std::unordered_map<int, int> map2rightcdr;
std::unordered_map<int, int> map2parent;
std::unordered_map<int, int> map2patthern;
std::vector<int> nodes_ordered; //unknown set as 0.
std::vector<int> terminals_index; //the position of n terminals in the ordered nodes
std::vector<int> junction_index; //the position of (n-1) junction nodes
std::vector<int> junctions;
std::vector<int> terminals;
std::vector<int> patterns;//the type of nodes
std::vector<int> fixed_verteces;
float time;
}Query_tree_fixed;
typedef struct Path{
std::vector<int> nodeIds;
float wgt;
}Path;
typedef struct Instance_Tree{
std::unordered_map<int, int> map2leftcdr;
std::unordered_map<int, int> map2rightcdr;
std::unordered_map<int, int> map2parent;
std::unordered_set<int> nodes;
//std::unordered_map<int> junctions;
float wgt;
}Instance_Tree;
typedef struct Instance_Tree_rep{
//when I see a seen node, call it a different name and insert as unseen.
std::unordered_map<int, int> map2leftcdr;
std::unordered_map<int, int> map2rightcdr;
std::unordered_map<int, int> map2parent;
std::unordered_set<int> nodes;
float wgt;
int repeat;
}Instance_Tree_rep;
typedef struct PQEntity_AStar{
int nodeIdx;
float wgt;
float key;
std::vector<int> path;
}PQEntity_AStar;
//comparator for the priority queue in dijkstra algorithm.
struct comparator_AStar{
bool operator()(PQEntity_AStar p1, PQEntity_AStar p2){
return p1.key > p2.key;
}
};
//adaptation for binary trees
typedef struct PQEntity_AStar_Tree{
int nodeIdx;
std::unordered_map<int, int> node2vertex;
int curId_inpattern;
float wgt;
float key;
//bool junction;
// std::vector<int> subtree;
Instance_Tree subtree;
}PQEntity_AStar_Tree;
//comparator for the priority queue in dijkstra algorithm.
struct comparator_AStar_Tree{
bool operator()(PQEntity_AStar_Tree p1, PQEntity_AStar_Tree p2){
return p1.key > p2.key;
}
};
//comparator for Path.
struct comparator{//put smallest weight on the top. used by Dijkstra
bool operator()(Path p1, Path p2){
return p1.wgt > p2.wgt;
}
};
struct comparator2{//put largest weight on the top. used by dfs.
bool operator()(Path p1, Path p2){
return p1.wgt < p2.wgt;
}
};
typedef struct QueryResult{
std::vector<Path> paths;
int numPaths;//num of legitimate paths.
int mem;//num of paths in memory at most.
int totalPaths;//sum up the number of paths for each nodes.
//quantify the searching space. Basically num of paths generated.
}QueryResult;
typedef struct QueryResultTrees{
std::vector<Instance_Tree> trees;
int numTrees;//num of candidate instances.
int mem;//num of candidates in memory at most.
int totalTrees;//
//quantify the searching space. Basically num of paths generated.
}QueryResultTrees;
typedef struct ProphetEntry{
std::vector<std::pair<int, float> > upwards;
std::vector<std::pair<int, float> > downwards;
// float est;//estimation or heuristics.
}ProphetEntry;
inline float calcWgt(std::vector<float> edgeVal, float queryTime){
/*Weight is static*/
float weight = edgeVal[0];
// std::cout<< "weight calculated: " << weight;
if ( weight < 0.00001) return 10000;
else return weight;
//weight is static.
#ifdef STATIC_WGT
cout<< "weight calculated: " << edgeVal[0];
return edgeVal[0];//weight is static.
#endif
/*For DBLP*/
#ifdef DBLP_WGT
if(edgeVal[1]>queryTime) return MAX_WEIGHT;
else return edgeVal[0]*(queryTime-edgeVal[1]+0.1)/4.0;
#endif
/*For StackOverFlow*/
//weights are based on the popularity of questions, answers and users.
#ifdef SOF_WGT_POP
if(edgeVal[1]>queryTime) return MAX_WEIGHT;
else return (10/edgeVal[0]);//one year.(original unit is hour)
#endif
//weight are based on the recency
#ifdef SOF_WGT_TIME
if(edgeVal[1]>queryTime) return MAX_WEIGHT;
else return (queryTime-edgeVal[1]+0.1)/(8640.0);//one year.(original unit is hour)
#endif
//combining the two
#ifdef SOF_WGT
if(edgeVal[1]>queryTime) return MAX_WEIGHT;
else return (10/edgeVal[0])*(queryTime-edgeVal[1]+0.1)/(8640.0);//one year.(original unit is hour)
#endif
/*For Enron. All edge weights are normalized to be 0-100.*/
#ifdef ENRON_WGT
if(edgeVal[0]>0) return edgeVal[0];
if(-edgeVal[0]>queryTime) return MAX_WEIGHT;
else return (queryTime+edgeVal[0]+0.1)/(604800.0);//1 week. (original unit is second)
#endif
}
// else return ((967765860+edgeVal[0]+10)/21057300.0)*100.0;
class graph_t
{
public:
int n;
//The vector node contains iterators to the vector neighbors, pointing to the first neighbor of that node. There are num_nodes entries in the node vector and num_edges entries in the neighbors vector.
std::vector<int> neighbors;// neighbors as one-d array .
std::vector<std::vector<float> > wgts;//weightes on edges.
std::vector<int> nodes;//incremental neighbors index. //XF: what is the difference between neighbors and nodes?
std::vector<int> typeMap;
std::vector<int> degree;//instead of doing query everytime, store degree in a vector.
};
#endif //GLOBAL_HEADER