Implement a parallel BFS algorithm using OpenMP for large graph traversal and compare its performance with the serial BFS algorithm.
Breadth-First Search (BFS) is a fundamental graph traversal algorithm. The goal is to parallelize BFS to handle large graphs more efficiently using multiple threads.
main.cpp: Contains the implementation of the serial and parallel BFS algorithms and the main function.CMakeLists.txt: For those using CMake to build the project.
- C++ compiler with OpenMP support (e.g., GCC, Clang)
- CMake (optional, if using the provided
CMakeLists.txt)
- Open a terminal.
- Navigate to the directory containing
main.cpp. - Compile the code using g++ with OpenMP support:
g++ -fopenmp -o parallel_bfs main.cpp
- Open a terminal.
- Navigate to the project directory containing the
CMakeLists.txtfile. - Create a build directory and navigate into it:
mkdir build cd build - Run CMake to generate the build files:
cmake ..
- Build the project:
cmake --build .
- After compilation, run the executable from the terminal:
./parallel_bfs
- The program will display the number of available threads and prompt you to enter the number of threads to use:
Number of available threads: 4 Enter the number of threads to use (1 to 4): - Enter the desired number of threads and press Enter.
- The program will then generate a random graph, perform both serial and parallel BFS, and display the execution times for each, along with the speedup achieved.
- Number of threads to use:
3
Number of available threads: 4
Enter the number of threads to use (1 to 4): 3
Number of threads set to: 3
Serial BFS Time: 0.0057253 seconds
Parallel BFS Time: 0.0068441 seconds
Serial BFS was faster.
Processed graph with 1000 nodes and 5000 edges.
Speedup: 0.836531
- Serial BFS Time: Time taken by the serial BFS algorithm.
- Parallel BFS Time: Time taken by the parallel BFS algorithm.
- Comparison: A message indicating which implementation was faster.
- Graph Details: Number of nodes and edges in the generated graph.
- Speedup: The ratio of serial BFS time to parallel BFS time, indicating the efficiency of parallelization.
- Graph Generation: The graph is randomly generated with a specified number of nodes and edges.
- Performance Variability: The performance and speedup may vary depending on the graph structure and the system's hardware.
- Ensure your compiler supports OpenMP. For GCC, this is typically included by default.
- If you encounter issues with CMake, verify that CMake is installed and properly configured in your system's PATH.