-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource1.cpp
More file actions
253 lines (245 loc) · 5.38 KB
/
Source1.cpp
File metadata and controls
253 lines (245 loc) · 5.38 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
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
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
#include <chrono>
#include <stack>
#include <deque>
#include <queue>
using namespace std;
vector<vector<int> > adj;
struct vertice {
size_t postOrder = 0;
size_t preOrder = 0;
size_t ccNum = 0;
size_t index = 0;
bool visited = false;
};
vertice* v;//record the status
int Max(int a, int b, int c)
{
int max;
if (a >= b)
{
if (a >= c) {
max = a;
}
else
max = c;
}
else if (b >= c) { max = b; }
else max = c;
return max;
}
void Input_data(const string& filename) {
std::fstream in(filename.c_str());
cout << "reading file:" << filename << endl;
string s;
size_t n = 0, m = 0;
string data1, data2;
while (true)
{
std::getline(in, s);
istringstream is(s);
is >> data1 >> data2;
int d1 = stoi(data1);
int d2 = stoi(data2);
n = Max(n, d2, d1);
m += 1;
if (in.eof()) { break; }
}
//this block will count the number of lines and calculate the maximun number appeared in the file, which are the parameters n, m(vertice, edge)
in.clear();
in.seekg(0, ios::beg);
n += 1;
m -= 1;
v = new vertice[n];
for (size_t i = 0; i < n; i++) {
v[i].index = i;
}
adj = vector<vector<int> >(n, vector<int>());
for (size_t i = 0; i < m; i++)
{
int x, y;
std::getline(in, s);
istringstream is(s);
is >> data1 >> data2;
x = stoi(data1);
y = stoi(data2);
adj[x].push_back(y);
}
in.close();
//this block will assign data into the vertice template in terms of the adjancancy list
}
void dfsStack() {
//using naive c++ stl stack
size_t n = adj.size();
for (size_t i = 0; i < n; i++)
{
v[i].visited = false;
}
stack<vertice> S;
for (size_t k = 0; k < n; k++) {
if (v[k].visited) { continue; }
else
v[k].visited = true;//start point v[0]
S.push(v[k]);
while (!S.empty())
{
vertice u = S.top();
S.pop();//these two steps represent the operation pop
size_t m = adj[u.index].size();
int index = -1;
for (size_t j = 0; j < m; j++)
{
size_t i = adj[u.index][j];
if (!v[i].visited)
{
index = i;
break;
}
}
if (index != -1)
{
S.push(u);
v[index].visited = true;
S.push(v[index]);
}
}
}
//S.~stack<vertice>();//release memory
}
void dfsDeque() {
size_t n = adj.size();
for (size_t i = 0; i < n; i++)
{
v[i].visited = false;
}
deque<vertice> D;
for (size_t k = 0; k < n; k++) {
if (v[k].visited) { continue; }
else
v[k].visited = true;//start point v[0]
D.push_front(v[k]);
while (!D.empty())
{
vertice u = D.front();
D.pop_front();
size_t m = adj[u.index].size();
int index = -1;
for (size_t j = 0; j < m; j++)
{
size_t i = adj[u.index][j];
if (!v[i].visited)
{
index = i;
break;
}
}
if (index != -1)
{
D.push_front(u);
v[index].visited = true;
D.push_front(v[index]);
}
}
}
//D.~deque<vertice>();//release memory
}
void bfsQueue()
{
size_t n = adj.size();
for (size_t i = 0; i < n; i++)
{
v[i].visited = false;
}
queue<vertice> Q;
for (size_t k = 0; k < n; k++) {
if (v[k].visited) {
continue;
}
else
v[k].visited = true;
Q.push(v[k]);
while (!Q.empty())
{
vertice u = Q.front();
Q.pop();//these two steps represent the operation dequeue()
size_t m = adj[u.index].size();
for (size_t j = 0; j < m; j++)
{
size_t i = adj[u.index][j];
if (!v[i].visited)
{
v[i].visited = true;
Q.push(v[i]);
}
}
}
}
//Q.~queue<vertice>();
}
void bfsDeque() {
size_t n = adj.size();
for (size_t i = 0; i < n; i++)
{
v[i].visited = false;
}
deque<vertice> D;
for (size_t k = 0; k < n; k++) {
if (v[k].visited) { continue; }
else
v[k].visited = true;
D.push_back(v[k]);
while (!D.empty())
{
vertice u = D.front();
D.pop_front();//these two steps represent the operation dequeue()
size_t m = adj[u.index].size();
for (size_t j = 0; j < m; j++)
{
size_t i = adj[u.index][j];
if (!v[i].visited)
{
v[i].visited = true;
D.push_back(v[i]);
}
}
}
}
//D.~deque<vertice>();
}
void write_output_dfs() {
ofstream exp_data("bipartite_test.txt", std::ios::app);// For the convenience of analyzing
auto start = chrono::steady_clock::now();
bfsDeque();
auto end1 = chrono::steady_clock::now();
dfsDeque();
auto end2 = chrono::steady_clock::now();
bfsQueue();
auto end3 = chrono::steady_clock::now();
dfsStack();
auto end4 = chrono::steady_clock::now();
std::chrono::duration<double> elapsed_time1 = end1 - start;
std::chrono::duration<double> elapsed_time2 = end2 - end1;
std::chrono::duration<double> elapsed_time3 = end3 - end2;
std::chrono::duration<double> elapsed_time4 = end4 - end3;
exp_data <<adj.size() << " " << elapsed_time1.count() * 1000 << " " << elapsed_time2.count() * 1000 << " "<< elapsed_time3.count() * 1000<< " " << elapsed_time4.count() * 1000<<endl;
exp_data.close();
}
void Print_data() {
int n = adj.size();
for (int i = 0; i < n; i++) {
int m = adj[i].size();
cout << i << ": ";
for (int j = 0; j < m; j++) { cout << adj[i][j] << " ,"; }
cout << endl;
}
}
int main() {
Input_data("out.txt");
//Print_data();
write_output_dfs();
}