-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstreaming_BFS.cpp
More file actions
219 lines (203 loc) · 4.36 KB
/
streaming_BFS.cpp
File metadata and controls
219 lines (203 loc) · 4.36 KB
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
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<list>
using namespace std;
//static int samp_size=50;
template <typename T>
class queue{
vector<T> array;
public:
void insert(T x){
array.push_back(x);
}
T remove(){
T x=array[0];
array.erase(array.begin());
return x;
}
void remove_time(map<string,int> m,int time){
T x=array[0];
while(m[x]<=time){
array.erase(array.begin());
x=array[0];
}
}
void remove_element(T x){
int cnt=0;
for(int i=0;i<array.size();i++){
if(array[i]==x)
break;
cnt++;
}
array.erase(array.begin()+cnt);
}
void display(){
cout << "displaying queue\n";
typename vector<T>::iterator i;
for(i=array.begin();i!= array.end();i++)
cout << *i << "\n";
}
int empty(){
if(array.size()==0)
return 1;
return 0;
}
int present(T x){
typename vector<T>::iterator i;
i=find(array.begin(),array.end(),x);
if(i!=array.end())
return 1;
else return 0;
}
int size(){
return array.size();
}
int sample_seed_node(){
list<int> temp;
int tmp[2];
int x;
for(int i=0;i<array.size();i++){
x=array[i].find("\t");
tmp[0]=stoi(array[i].substr(0,x));
tmp[1]=stoi(array[i].substr(x+1,array[i].length()));
temp.push_back(tmp[0]);
temp.push_back(tmp[1]);
}
temp.unique();
int randNum = rand()%(temp.size());
auto l=temp.begin();
advance(l,randNum);
return *l;
}
int find_incedent_edge(int x){
vector<int> temp;
int tmp[2],y;
for(int i=0;i<array.size();i++){
y=array[i].find("\t");
tmp[0]=stoi(array[i].substr(0,y));
tmp[1]=stoi(array[i].substr(y+1,array[i].length()));
if(tmp[0]==x)
temp.push_back(tmp[1]);
if(tmp[1]==x)
temp.push_back(tmp[0]);
}
//cout << "length: "<< temp.size() << endl;
if(temp.size()>0)
return temp[rand()%temp.size()];
return -1;
}
};
string edge_format(int a, int b){
string e_t;
if(a<b)
e_t=to_string(a)+"\t"+to_string(b);
else e_t=to_string(b)+"\t"+to_string(a);
return e_t;
}
int is_present(vector<int> w, int n){
vector<int>::iterator it;
it=find(w.begin(),w.end(),n);
if(it!=w.end())
return 1;
else return 0;
}
int present(vector<string> w,string e){
vector<string>::iterator it;
it=find(w.begin(),w.end(),e);
if(it!=w.end())
return 1;
else return 0;
}
void adjust(vector<int> &node, vector<string> &edge, int samp_size, char *name){
int max,u,x;
int tmp[2];
vector<int> temp;
while(node.size()>samp_size){
max=node.size()-1;
int randNum=rand()%max;
u=node[randNum];
node.erase(std::remove(node.begin(),node.end(),u),node.end());
temp.push_back(u);
}
ofstream out;
string fname = "sample_BFS_";
fname.append(name);
out.open(fname);
for(int i=0;i<edge.size();i++)
{
x=edge[i].find("\t");
tmp[0]=stoi(edge[i].substr(0,x));
tmp[1]=stoi(edge[i].substr(x+1,edge[i].length()));
if((is_present(temp,tmp[0]))||(is_present(temp,tmp[1])))
continue;
else out << edge[i] << endl;
}
out.close();
}
int main(int argc, char* argv[]){
queue<string> window;
queue<int> node_queue;
map<string,int> edge_time;
int t1=atoi(argv[4]);
int t_stop = atoi(argv[5]); // t_stop >> t1
int population = atoi(argv[2]);
int samp_size = (int)(population*atof(argv[3]));
string e_t, e_s;
int a,b,u,t,v,x,cnt=0,flag=0;
vector<int> node;
vector<string> edge;
ifstream in;
in.open(argv[1]); // the graph file should be in the format [time \t node \t node]
while(in >> t >> a >> b){
cout << "streaming edge: " << a << " " << b << " time: " << t << endl;
if(t>t_stop)
break;
e_t=edge_format(a,b);
edge_time[e_t]=t;
if(t < t1)
{
if(!window.present(e_t)){
window.insert(e_t);
cnt++;
}
}
else{
if(flag==0){
flag++;
window.insert(e_t);
window.display();
u=window.sample_seed_node();
node.push_back(u);
}
else{
v=window.find_incedent_edge(u);
if(v==-1){ // no incedent edge on u present
if(!node_queue.empty()){
u=node_queue.remove();
}
else{
u=window.sample_seed_node();
}
if(!is_present(node,u))
node.push_back(u);
}
else{
e_s=edge_format(u,v);
edge.push_back(e_s);
if(!is_present(node,v))
node.push_back(v);
window.remove_element(e_s);
node_queue.insert(v);
}
window.remove_time(edge_time,t-t1);
window.insert(e_t);
}
}
}
adjust(node,edge,samp_size,argv[3]);
return 0;
}