0% found this document useful (0 votes)
12 views3 pages

Bfs

The document provides a C++ implementation of a breadth-first search (BFS) algorithm using OpenMP for parallel processing. It includes a class for tree nodes and methods for inserting nodes and performing BFS traversal. The main function allows user input for node values and displays the BFS traversal result.

Uploaded by

yaseeniqbal365
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Bfs

The document provides a C++ implementation of a breadth-first search (BFS) algorithm using OpenMP for parallel processing. It includes a class for tree nodes and methods for inserting nodes and performing BFS traversal. The main function allows user input for node values and displays the BFS traversal result.

Uploaded by

yaseeniqbal365
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Code to implement BFS using OpenMP:

#include <iostream>
#include <queue>
#include <omp.h>
using namespace std;

class Node {
public:
int data;
Node* left;
Node* right;
};

class BreadthFS {
public:
Node* insert(Node* root, int data);
void bfs(Node* root);
};

// Function to insert a node in level-order


Node* BreadthFS::insert(Node* root, int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->left = newNode->right = nullptr;

if (!root)
return newNode;

queue<Node*> q;
q.push(root);

while (!q.empty()) {
Node* temp = q.front();
q.pop();

if (!temp->left) {
temp->left = newNode;
return root;
} else {
q.push(temp->left);
}

if (!temp->right) {
temp->right = newNode;
return root;
} else {
q.push(temp->right);
}
}
return root;
}

// Parallel BFS using OpenMP


void BreadthFS::bfs(Node* root) {
if (!root)
return;

queue<Node*> q;
q.push(root);

cout << "BFS traversal:\n";

while (!q.empty()) {
int qSize = q.size();

// Parallel loop over current level


#pragma omp parallel for
for (int i = 0; i < qSize; i++) {
Node* currNode;

// Critical section for queue access


#pragma omp critical
{
if (!q.empty()) {
currNode = q.front();
q.pop();
cout << "\t" << currNode->data;

if (currNode->left)
q.push(currNode->left);
if (currNode->right)
q.push(currNode->right);
}
}
}
}

cout << endl;


}

int main() {
Node* root = nullptr;
BreadthFS bfsTree;
int data;
char ans;

do {
cout << "Enter data => ";
cin >> data;
root = bfsTree.insert(root, data);
cout << "Do you want to insert one more node? (y/n): ";
cin >> ans;
} while (ans == 'y' || ans == 'Y');

bfsTree.bfs(root);

return 0;
}

Output:
Enter data => 5
Do you want to insert one more node? (y/n) y
Enter data => 3
Do you want to insert one more node? (y/n) y
Enter data => 2
Do you want to insert one more node? (y/n) y
Enter data => 1
Do you want to insert one more node? (y/n) y
Enter data => 7
Do you want to insert one more node? (y/n) y
Enter data => 8
Do you want to insert one more node? (y/n) n
5 3 7 2 1 8

You might also like