Skip to content

Commit a71e510

Browse files
authored
Add files via upload
0 parents  commit a71e510

File tree

5 files changed

+696
-0
lines changed

5 files changed

+696
-0
lines changed

1_BFS_DFS.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <queue>
4+
#include <omp.h>
5+
6+
using namespace std;
7+
8+
// Graph class representing the adjacency list
9+
class Graph {
10+
int V; // Number of vertices
11+
vector<vector<int>> adj; // Adjacency list
12+
13+
public:
14+
Graph(int V) : V(V), adj(V) {}
15+
16+
17+
void addEdge(int v, int w) {
18+
adj[v].push_back(w);
19+
}
20+
21+
// Parallel Depth-First Search
22+
void parallelDFS(int startVertex) {
23+
vector<bool> visited(V, false);
24+
parallelDFSUtil(startVertex, visited);
25+
}
26+
27+
// Parallel DFS utility function
28+
void parallelDFSUtil(int v, vector<bool>& visited) {
29+
visited[v] = true;
30+
cout << v << " ";
31+
32+
#pragma omp parallel for
33+
for (int i = 0; i < adj[v].size(); ++i) {
34+
int n = adj[v][i];
35+
if (!visited[n])
36+
parallelDFSUtil(n, visited);
37+
}
38+
}
39+
40+
// Parallel Breadth-First Search
41+
void parallelBFS(int startVertex) {
42+
vector<bool> visited(V, false);
43+
queue<int> q;
44+
45+
visited[startVertex] = true;
46+
q.push(startVertex);
47+
48+
while (!q.empty()) {
49+
int v = q.front();
50+
q.pop();
51+
cout << v << " ";
52+
53+
#pragma omp parallel for
54+
for (int i = 0; i < adj[v].size(); ++i) {
55+
int n = adj[v][i];
56+
if (!visited[n]) {
57+
visited[n] = true;
58+
q.push(n);
59+
}
60+
}
61+
}
62+
}
63+
};
64+
65+
int main() {
66+
// Create a graph
67+
Graph g(7);
68+
g.addEdge(0, 1);
69+
g.addEdge(0, 2);
70+
g.addEdge(1, 3);
71+
g.addEdge(1, 4);
72+
g.addEdge(2, 5);
73+
g.addEdge(2, 6);
74+
75+
76+
cout << "Depth-First Search (DFS): ";
77+
g.parallelDFS(0);
78+
cout << endl;
79+
80+
cout << "Breadth-First Search (BFS): ";
81+
g.parallelBFS(0);
82+
cout << endl;
83+
84+
return 0;
85+
}

2_parallel.cpp

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#include <bits/stdc++.h>
2+
#include<omp.h>
3+
using namespace std;
4+
void bubble(int array[], int n){
5+
for (int i = 0; i < n - 1; i++){
6+
for (int j = 0; j < n - i - 1; j++){
7+
if (array[j] > array[j + 1]) swap(array[j], array[j + 1]);
8+
}
9+
}
10+
}
11+
void parallel_bubblesort(int array[], int n) {
12+
for (int i = 0; i < n; i++)
13+
{
14+
int first = i % 2;
15+
// defining the shared data source for parallel execution.
16+
#pragma omp parallel for shared (array, first) num_threads(16)
17+
for (int j = first; j < n - 1; j += 2)
18+
if (array[j] > array[j + 1])
19+
swap(array[j], array[j + 1]);
20+
}
21+
return;
22+
}
23+
void merge(int arr[], int low, int mid, int high) {
24+
// Create arrays of left and right partititons
25+
int n1 = mid - low + 1;
26+
int n2 = high - mid;
27+
28+
int left[n1];
29+
int right[n2];
30+
31+
// Copy all left elements
32+
for (int i = 0; i < n1; i++) left[i] = arr[low + i];
33+
34+
// Copy all right elements
35+
for (int j = 0; j < n2; j++) right[j] = arr[mid + 1 + j];
36+
37+
// Compare and place elements
38+
int i = 0, j = 0, k = low;
39+
40+
while (i < n1 && j < n2) {
41+
if (left[i] <= right[j]){
42+
arr[k] = left[i];
43+
i++;
44+
}
45+
else{
46+
arr[k] = right[j];
47+
j++;
48+
}
49+
k++;
50+
}
51+
52+
// If any elements are left out
53+
while (i < n1) {
54+
arr[k] = left[i];
55+
i++;
56+
k++;
57+
}
58+
59+
while (j < n2) {
60+
arr[k] = right[j];
61+
j++;
62+
k++;
63+
}
64+
}
65+
void mergeSort(int arr[], int low, int high) {
66+
if (low < high) {
67+
int mid = (low + high) / 2;
68+
mergeSort(arr, low, mid);
69+
mergeSort(arr, mid + 1, high);
70+
merge(arr, low, mid, high);
71+
}
72+
}
73+
74+
void parallelMergeSort(int arr[], int low, int high) {
75+
if (low < high) {
76+
int mid = (low + high) / 2;
77+
78+
#pragma omp parallel sections
79+
{
80+
#pragma omp section
81+
{
82+
mergeSort(arr, low, mid);
83+
}
84+
85+
#pragma omp section
86+
{
87+
mergeSort(arr, mid + 1, high);
88+
}
89+
}
90+
merge(arr, low, mid, high);
91+
}
92+
}
93+
int main(){
94+
// Set up variables
95+
int n = 100000;
96+
int arr[n];
97+
int brr[n],crr[n],drr[n];
98+
double start_time, end_time;
99+
// Create an array with random numbers
100+
for(int i = 0, j = n; i < n; i++, j--){
101+
arr[i] = 1 + (rand() % 1000);
102+
brr[i]=arr[i];
103+
crr[i]=arr[i];
104+
drr[i]=arr[i];
105+
}
106+
bool flag = true;
107+
while(flag){
108+
cout << "-------------------------- Menu -------------------------------\n";
109+
cout << "1. Bubble Sort \n" ;
110+
cout << "2. Merge Sort \n" << endl;
111+
cout << "3. Exit \n";
112+
cout << "---------------------------------------------------------------\n";
113+
int choice =-1;
114+
cout<<"Enter your choice: ";
115+
cin>>choice;
116+
switch(choice)
117+
{
118+
case 1:
119+
cout << "Bubble Sort\n";
120+
// Sequential time
121+
start_time = omp_get_wtime();
122+
bubble(arr, n);
123+
end_time = omp_get_wtime();
124+
cout << "Sequential Bubble Sort took : " << end_time - start_time << " seconds.\n";
125+
126+
// Parallel time
127+
start_time = omp_get_wtime();
128+
parallel_bubblesort(brr, n);
129+
end_time = omp_get_wtime();
130+
cout << "Parallel Bubble Sort took : " << end_time - start_time << " seconds.\n";
131+
break;
132+
case 2:
133+
cout << "Merge Sort\n";
134+
// Sequential time
135+
start_time = omp_get_wtime();
136+
mergeSort(crr, 0, n - 1);
137+
end_time = omp_get_wtime();
138+
cout << "Sequential Merge Sort took : " << end_time - start_time << " seconds.\n";
139+
140+
// Parallel time
141+
start_time = omp_get_wtime();
142+
parallelMergeSort(drr, 0, n - 1);
143+
end_time = omp_get_wtime();
144+
cout << "Parallel Merge Sort took : " << end_time - start_time << " seconds.\n";
145+
break;
146+
case 3:
147+
flag = false;
148+
break;
149+
default:
150+
cout<<"Invalid choice\n";
151+
break;
152+
}
153+
}
154+
return 0;
155+
156+
}

3_parallel_reduction.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
2+
#include<bits/stdc++.h>
3+
#include<omp.h>
4+
5+
using namespace std;
6+
// Function to find the minimum value in the array
7+
int minval(int arr[], int n){
8+
int minval = arr[0];
9+
#pragma omp parallel for reduction(min : minval)
10+
for(int i = 0; i < n; i++){
11+
if(arr[i] < minval) minval = arr[i];
12+
}
13+
return minval;
14+
}
15+
// Function to find the maximum value in the array
16+
int maxval(int arr[], int n){
17+
int maxval = arr[0];
18+
#pragma omp parallel for reduction(max : maxval)
19+
for(int i = 0; i < n; i++){
20+
if(arr[i] > maxval) maxval = arr[i];
21+
}
22+
return maxval;
23+
}
24+
// Function to find the sum of the elements in the array
25+
int sum(int arr[], int n){
26+
int sum = 0;
27+
#pragma omp parallel for reduction(+ : sum)
28+
for(int i = 0; i < n; i++){
29+
sum += arr[i];
30+
}
31+
return sum;
32+
}
33+
// Function to find the average of the elements in the array
34+
int average(int arr[], int n){
35+
return (double)sum(arr, n) / n;
36+
}
37+
38+
int main(){
39+
40+
int n;
41+
cout<<"Enter the number of elements: ";
42+
cin>>n;
43+
int arr[n];
44+
cout<<"Enter the elements: ";
45+
for(int i=0;i<n;i++){
46+
arr[i] = 1 + (rand() % n);
47+
}
48+
bool flag = true;
49+
while(flag){
50+
cout << "-------------------------- Menu -------------------------------" << endl;
51+
cout << "1. Sequential \n";
52+
cout << "2. Parallel\n";
53+
cout<< "3. Exit\n";
54+
cout << "---------------------------------------------------------------" << endl;
55+
int choice =-1;
56+
cout<<"Enter your choice: ";
57+
cin>>choice;
58+
double start_time = omp_get_wtime();
59+
switch(choice){
60+
case 1:
61+
{cout<<"Sequential\n";
62+
cout << "The minimum value is: " << *min_element(arr,arr+ n) << '\n';
63+
cout<< "The maximum value is: "<< *max_element(arr,arr+n) << '\n';
64+
int sum1=0;
65+
for(int i=0;i<n;i++){
66+
sum1+=arr[i];
67+
}
68+
cout<<"The summation is: "<<sum1<<'\n';
69+
cout<<"The average is: "<<(double)sum1/(double)n<<'\n';
70+
break;}
71+
case 2:
72+
{cout<<"Parallel\n";
73+
cout<<"The minimum value is: "<<minval(arr,n)<<'\n';
74+
cout << "The maximum value is: " << maxval(arr, n) << '\n';
75+
cout << "The summation is: " << sum(arr, n) << '\n';
76+
cout << "The average is: " << average(arr, n) << '\n';
77+
break;}
78+
case 3:
79+
flag = false;
80+
break;
81+
default:
82+
cout << "Invalid choice\n";
83+
}
84+
double end_time = omp_get_wtime();
85+
cout<<"Time taken: "<<end_time-start_time<<" seconds\n";
86+
}
87+
return 0;
88+
}

0 commit comments

Comments
 (0)