-
Notifications
You must be signed in to change notification settings - Fork 0
/
LP5 HPC Assignment 1B (2).cpp
64 lines (52 loc) · 1.16 KB
/
LP5 HPC Assignment 1B (2).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
// Cedrick Andrade
// COBA006
#include <iostream>
#include <vector>
#include <stack>
#include <omp.h>
using namespace std;
const int MAXN = 10000; // maximum number of nodes
vector<int> adjList[MAXN];
bool visited[MAXN];
void dfs(int node) {
visited[node] = true;
for (int i = 0; i < adjList[node].size(); i++) {
int neighbor = adjList[node][i];
if (!visited[neighbor]) {
dfs(neighbor);
}
}
}
int main() {
int n, m;
cin >> n >> m;
// read in the graph
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adjList[u].push_back(v);
adjList[v].push_back(u);
}
// initialize visited array to false
#pragma omp parallel for
for (int i = 0; i < n; i++) {
visited[i] = false;
}
// set up the DFS starting point
int source = 0;
// do DFS in parallel
#pragma omp parallel
{
#pragma omp single
{
dfs(source);
}
}
// print out the visited nodes
for (int i = 0; i < n; i++) {
if (visited[i]) {
cout << "Node " << i << " is visited." << endl;
}
}
return 0;
}