-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs
More file actions
60 lines (58 loc) · 1.59 KB
/
dfs
File metadata and controls
60 lines (58 loc) · 1.59 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
// Parallel DFS using OpenMP
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define MAX 100 // Maximum number of vertices
int graph[MAX][MAX]; // Adjacency matrix
int visited[MAX]; // Visited array
int V; // Number of vertices
// DFS Function
void dfs(int v) {
#pragma omp critical
{
if (visited[v]) return; // Avoid revisiting nodes
visited[v] = 1;
printf("Visited %d\n", v);
}
#pragma omp parallel for
for (int i = 0; i < V; i++) {
if (graph[v][i] && !visited[i]) {
#pragma omp task
dfs(i);
}
}
#pragma omp taskwait // Wait for all tasks to complete
}
int main() {
int E, u, v; // E: number of edges
printf("Enter the number of vertices: ");
scanf("%d", &V);
printf("Enter the number of edges: ");
scanf("%d", &E);
// Initialize adjacency matrix and visited array
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
graph[i][j] = 0;
}
visited[i] = 0;
}
// Input edges
printf("Enter edges (format: u v):\n");
for (int i = 0; i < E; i++) {
scanf("%d %d", &u, &v);
graph[u][v] = 1;
graph[v][u] = 1; // Assuming undirected graph
}
int start_vertex;
printf("Enter the starting vertex for DFS: ");
scanf("%d", &start_vertex);
double start_time = omp_get_wtime();
#pragma omp parallel
{
#pragma omp single
dfs(start_vertex);
}
double end_time = omp_get_wtime();
printf("DFS completed in %f seconds\n", end_time - start_time);
return 0;
}