0% found this document useful (0 votes)
1 views1 page

PR 4

The document contains a C++ implementation of the Breadth-First Search (BFS) algorithm using OpenMP for parallel processing. It defines a graph as an adjacency matrix and performs BFS traversal starting from a specified source vertex. The code includes mechanisms to track visited vertices and manage concurrent access to shared resources.

Uploaded by

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

PR 4

The document contains a C++ implementation of the Breadth-First Search (BFS) algorithm using OpenMP for parallel processing. It defines a graph as an adjacency matrix and performs BFS traversal starting from a specified source vertex. The code includes mechanisms to track visited vertices and manage concurrent access to shared resources.

Uploaded by

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

#include <iostream>

#include <vector>
#include <queue>
#include <omp.h>

using namespace std;

#define V 6

void bfs(int graph[V][V], int src) {


vector<bool> visited(V, false); // Track visited vertices
queue<int> q;

visited[src] = true;
q.push(src);

cout << "BFS Traversal starting from vertex " << src << ":" << endl;

while (!q.empty()) {
int u = q.front();
q.pop();
cout << u << " ";

#pragma omp parallel for


for (int v = 0; v < V; v++) {
if (graph[u][v] && !visited[v]) {
#pragma omp critical
{
if (!visited[v]) {
visited[v] = true;
q.push(v);
}
}
}
}
}
}

int main() {
// Example graph represented as an adjacency matrix
int graph[V][V] = {
{0, 1, 0, 1, 0, 0},
{1, 0, 1, 0, 0, 0},
{0, 1, 0, 1, 1, 0},
{1, 0, 1, 0, 1, 0},
{0, 0, 1, 1, 0, 1},
{0, 0, 0, 0, 1, 0}
};

// Source vertex
int src = 0;

bfs(graph, src);

return 0;
}

You might also like