Advertisement
sepi0l

hpc_parabfs

Apr 25th, 2024
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.33 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<omp.h>
  4. #include<queue>
  5. #include<bits/stdc++.h>
  6. using namespace std;
  7.  
  8. queue<int>q;
  9.  
  10. void bfs(int start, int* arr, int n, int visit[])
  11. {
  12.     #pragma omp parallel for ordered
  13.     for(int i=0; i<n; i++)
  14.     {
  15.         #pragma omp ordered
  16.         if( ( *(arr + (n*start) + i)  == 1 ) && (visit[i] == 0) )
  17.         {
  18.             cout<<i<<" ";
  19.             q.push(i);
  20.             visit[i] = 1;
  21.         }
  22.     }
  23.  
  24.     q.pop();
  25.  
  26.     if(!q.empty()) bfs(q.front(), (int*)arr, n, visit);
  27.    
  28. }
  29.  
  30. int main()
  31. {
  32.  
  33.     //freopen("input.txt","r",stdin);
  34.     //freopen("output.txt","w",stdout);
  35.  
  36.     //cout<<"BFS 0 1 2 3 4 5 6"<<endl;
  37.     cout<<"Enter the number of vertices: ";
  38.     int n;
  39.     cin>>n;
  40.  
  41.     int arr[n][n] = {0};
  42.  
  43.     cout<<"Enter the number of edges: ";
  44.     int edges;
  45.     cin>>edges;
  46.  
  47.     for(int j=0; j<edges; j++)
  48.     {
  49.         int a, b;
  50.         cout<<"Enter the two edges:"<<endl;
  51.         cin>>a>>b;
  52.         arr[a][b] = 1;
  53.         arr[b][a] = 1;
  54.     }
  55.  
  56.     int visit[n] = {0};
  57.  
  58.     cout<<"Enter the start vertex: ";
  59.     int start;
  60.     cin>>start;
  61.    
  62.    
  63.     clock_t strt = clock();
  64.  
  65.     visit[start] = 1;
  66.     cout<<start<<" ";
  67.     q.push(start);
  68.  
  69.     bfs(start, (int*)arr, n, visit);
  70.  
  71.     clock_t stop = clock();
  72.  
  73.     cout<<"\nTime required : "<<(double)(stop-strt)<<" ms"<<endl;
  74.  
  75.     return 0;
  76. }
  77.  
  78. /*
  79.  
  80. "Parallel Execution"
  81. PS D:\C++> g++ -fopenmp parallel_bfs.cpp
  82. PS D:\C++> ./a out
  83. Enter the number of vertices: 7
  84. Enter the number of edges: 6
  85. Enter the two edges:
  86. 0 1
  87. Enter the two edges:
  88. 0 2
  89. Enter the two edges:
  90. 1 3
  91. Enter the two edges:
  92. 1 4
  93. Enter the two edges:
  94. 2 5
  95. Enter the two edges:
  96. 2 6
  97. Enter the start vertex: 0
  98. 0 1 2 3 4 5 6
  99. Time required : 3 ms
  100.  
  101. "Serial Execution"
  102. PS D:\C++> g++ parallel_bfs.cpp
  103. PS D:\C++> ./a out
  104. Enter the number of vertices: 7
  105. Enter the number of edges: 6
  106. Enter the two edges:
  107. 0 1
  108. Enter the two edges:
  109. 0 2
  110. Enter the two edges:
  111. 1 3
  112. Enter the two edges:
  113. 1 4
  114. Enter the two edges:
  115. 2 5
  116. Enter the two edges:
  117. 2 6
  118. Enter the start vertex: 0
  119. 0 1 2 3 4 5 6
  120. Time required : 11 ms
  121.  
  122.  
  123. */
  124.  
  125. /*
  126. This code implements a Breadth-First Search (BFS) algorithm for a graph represented as an adjacency matrix. Let's analyze it:
  127.  
  128. 1. **`bfs` Function:**
  129.    - The function `bfs` performs a Breadth-First Search traversal starting from a given vertex.
  130.    - It iterates over the vertices adjacent to the `start` vertex in parallel using OpenMP directives.
  131.    - Each thread checks if an adjacent vertex is reachable from the `start` vertex and if it has been
  132.    visited before. If not visited, it prints the vertex, marks it as visited, and enqueues it.
  133.    - The traversal continues by dequeuing the next vertex from the queue and recursively calling `bfs`
  134.    until the queue is empty.
  135.  
  136. 2. **`main` Function:**
  137.    - It initializes the graph as an adjacency matrix `arr`, where `arr[i][j]` is 1 if there is an edge
  138.    between vertices `i` and `j`.
  139.    - User input is taken for the number of vertices `n`, the number of edges `edges`, and the edges themselves.
  140.    - BFS traversal is initiated from a user-defined starting vertex.
  141.    - The time taken for the BFS traversal is measured using the `clock` function.
  142.  
  143. 3. **Parallel Execution:**
  144.    - OpenMP directives (`#pragma omp parallel for ordered`) are used to parallelize the loop that iterates
  145.    over adjacent vertices.
  146.    - The `ordered` pragma ensures that the traversal order is maintained while still allowing parallel execution.
  147.  
  148. 4. **Output:**
  149.    - The BFS traversal sequence starting from the specified vertex is printed.
  150.    - The time taken for the BFS traversal is also printed in milliseconds.
  151.  
  152. 5. **Performance Consideration:**
  153.    - Parallelizing BFS traversal can improve performance by utilizing multiple threads to explore adjacent
  154.     vertices concurrently.
  155.    - However, the efficiency of parallelization depends on factors such as the number of vertices, edges, and
  156.    the distribution of edges in the graph.
  157.  
  158. 6. **Time Complexity:**
  159.    - The time complexity of BFS is O(V + E), where V is the number of vertices and E is the number of edges.
  160.    - In this implementation, parallelization aims to improve performance but does not change the time complexity
  161.     of the BFS algorithm.
  162.  
  163. Overall, this code demonstrates how Breadth-First Search can be implemented for graph traversal using OpenMP to
  164. potentially improve performance through parallel execution.
  165. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement