0% found this document useful (0 votes)
3 views6 pages

HPC 123

The document contains C++ code implementing a graph class with parallel breadth-first and depth-first search algorithms, as well as parallel sorting algorithms including bubble sort and merge sort. It also includes functions for parallel computation of minimum, maximum, sum, and average values from a vector of integers. The main function demonstrates the usage of these algorithms and functions with user input and random data generation.
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)
3 views6 pages

HPC 123

The document contains C++ code implementing a graph class with parallel breadth-first and depth-first search algorithms, as well as parallel sorting algorithms including bubble sort and merge sort. It also includes functions for parallel computation of minimum, maximum, sum, and average values from a vector of integers. The main function demonstrates the usage of these algorithms and functions with user input and random data generation.
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/ 6

#include <iostream>

#include <vector>
#include <queue>
#include <stack>
#include <omp.h>
using namespace std;
class Graph {
int V;
vector<int>* adj;
public:
Graph(int V);
void addEdge(int v, int w);
void parallelBFS(int s);
void parallelDFS(int s);
};
Graph::Graph(int V) {
this->V = V;
adj = new vector<int>[V];
}
void Graph::addEdge(int v, int w) {
adj[v].push_back(w);
adj[w].push_back(v);
}
void Graph::parallelBFS(int s) {
bool* visited = new bool[V];
for (int i = 0; i < V; i++) {
visited[i] = false;
}
queue<int> q;
visited[s] = true;
q.push(s);
while (!q.empty()) {
s = q.front();
cout << s << " ";
q.pop();
#pragma omp parallel for
for (auto i = adj[s].begin(); i != adj[s].end(); ++i) {
if (!visited[*i]) {
visited[*i] = true;
q.push(*i);
}
}
}
}
void Graph::parallelDFS(int s) {
bool* visited = new bool[V];
for (int i = 0; i < V; i++) {
visited[i] = false;
}
stack<int> st;
st.push(s);
while (!st.empty()) {
s = st.top();
st.pop();
if (!visited[s]) {
cout << s << " ";
visited[s] = true;
}
#pragma omp parallel for
for (auto i = adj[s].begin(); i != adj[s].end(); ++i) {
if (!visited[*i]) {
st.push(*i);
}
}
}
}
int main() {
int n;
cout << "Enter the number of vertices: ";
cin >> n;
Graph g(n);
int e;
cout << "Enter the number of edges: ";
cin >> e;
for (int i = 0; i < e; i++) {
int a, b;
cout << "Enter the vertices of edge (space separated):" << i + 1 << ": ";
cin >> a >> b;
g.addEdge(a, b);
}
cout << "Parallel Breadth First Traversal:\n";
g.parallelBFS(0);
cout << "\nParallel Depth First Traversal:\n";
g.parallelDFS(0);
return 0;
}
#include <iostream>
#include <omp.h>
void parallelBubbleSort(int arr[], int n) {
#pragma omp parallel
{
for (int i = 0; i < n - 1; i++) {
#pragma omp for
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
std::swap(arr[j], arr[j + 1]);
}
}
}
}
}
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int left[n1], right[n2];
for (int i = 0; i < n1; i++) {
left[i] = arr[l + i];
}
for (int j = 0; j < n2; j++) {
right[j] = arr[m + 1 + j];
}
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (left[i] <= right[j]) {
arr[k] = left[i];
i++;
}
else {
arr[k] = right[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = left[i];
i++;
k++;
}
while (j < n2) {
arr[k] = right[j];
j++;
k++;
}
}
void parallelMergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
#pragma omp parallel sections
{
#pragma omp section
parallelMergeSort(arr, l, m);
#pragma omp section
parallelMergeSort(arr, m + 1, r);
}
merge(arr, l, m, r);
}
}

int main() {
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
int n = sizeof(arr) / sizeof(arr[0]);
parallelBubbleSort(arr, n);
std::cout << "Sorted array using bubble sort: ";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
int arr2[] = { 64, 34, 25, 12, 22, 11, 90 };
int n2 = sizeof(arr) / sizeof(arr[0]);
parallelMergeSort(arr2, 0, n2 - 1);
std::cout << "\nSorted array using merge sort: ";
for (int i = 0; i < n2; i++) {
std::cout << arr2[i] << " ";
}
return 0;
}

Output:
#include <iostream>
#include <vector>
#include <omp.h>

template<typename T>
T parallelMin(const std::vector<T>& data) {
T result = data[0];
#pragma omp parallel for reduction(min: result)
for (size_t i = 1; i < data.size(); i++) {
result = std::min(result, data[i]);
}

return result;
}

template<typename T>
T parallelMax(const std::vector<T>& data) {
T result = data[0];
#pragma omp parallel for reduction(max: result)
for (size_t i = 1; i < data.size(); i++) {
result = std::max(result, data[i]);
}
return result;
}

template<typename T>
T parallelSum(const std::vector<T>& data) {
T result = 0;
#pragma omp parallel for reduction(+: result)
for (size_t i = 0; i < data.size(); i++) {
result += data[i];
}
return result;
}

template<typename T>
double parallelAverage(const std::vector<T>& data) {
T sum = parallelSum(data);
return static_cast<double>(sum) / data.size();
}

int main() {
std::vector<int> data;
for (int i = 0; i < 100; i++) {
data.push_back(rand() % 100);
}

std::cout << "Random Data: ";


for (auto i : data) {
std::cout << i << " ";
}
int minResult = parallelMin(data);
std::cout << "Min: " << minResult << std::endl;
int maxResult = parallelMax(data);
std::cout << "Max: " << maxResult << std::endl;
int sumResult = parallelSum(data);
std::cout << "Sum: " << sumResult << std::endl;
double averageResult = parallelAverage(data);
std::cout << "Average: " << averageResult << std::endl;
return 0;
}

You might also like