-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserial_bfs.cpp
executable file
·83 lines (65 loc) · 1.55 KB
/
serial_bfs.cpp
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
#include <iostream>
#include <vector>
#include <queue>
#include <chrono>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[]) {
if(argc < 3) {
cout<<"Expecting a file as command line arguement...";
return 0;
}
freopen(argv[1], "r", stdin);
int n,m;
cin>>n>>m;
vector<vector<int> > adj(n, vector<int>());
int prev;
int *number_of_edges = (int*)malloc(n*sizeof(int));
cin>>prev;
for(int i = 0; i < n; i++) {
int temp;
cin>>temp;
number_of_edges[i] = temp-prev;
prev = temp;
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < number_of_edges[i]; j++) {
int temp;
cin>>temp;
fflush(stdout);
adj[i].push_back(temp);
}
}
queue<int> q;
int *d = (int*)malloc(n*sizeof(int));
for(int i = 0; i < n; i++) d[i] = 1e9;
bool *vis = (bool*)malloc(n*sizeof(bool));
for(int i = 0; i < n; i++) vis[i] = false;
q.push(0);
vis[0] = true;
d[0] = 0;
int depth = 0;
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
while(!q.empty()) {
int v = q.front();
q.pop();
for(auto u:adj[v]) {
if(!vis[u]) {
vis[u] = true;
q.push(u);
d[u] = d[v]+1;
if(d[u] > depth) {
depth = d[u];
}
}
}
}
cout<<"The depth of the given graph from node 0 is "<<depth<<endl;
std::chrono::steady_clock::time_point end= std::chrono::steady_clock::now();
cout<<"Time taken for serial BFS: "<<(std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count())/10<<"ms\n";
freopen(argv[2], "w", stdout);
for(int i = 0; i < n; i++) {
cout<<d[i]<<endl;
}
return 0;
}