0% found this document useful (0 votes)
7 views

Dijkstra Algorithm using OpenMP

The document presents an implementation of Dijkstra's algorithm using OpenMP for parallel processing. It includes a function to find the vertex with the minimum distance and updates the shortest paths in a graph represented by an adjacency matrix. The program measures and outputs the time taken to compute the shortest distances from a source vertex.

Uploaded by

siddartha.ps2067
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)
7 views

Dijkstra Algorithm using OpenMP

The document presents an implementation of Dijkstra's algorithm using OpenMP for parallel processing. It includes a function to find the vertex with the minimum distance and updates the shortest paths in a graph represented by an adjacency matrix. The program measures and outputs the time taken to compute the shortest distances from a source vertex.

Uploaded by

siddartha.ps2067
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/ 2

Dijkstra Algorithm using OpenMP

#include <iostream>
#include <limits.h>
#include <omp.h>
#include <chrono>

#define INF INT_MAX


#define V 6 // Number of vertices

using namespace std;


using namespace chrono;

// Function to find the vertex with the minimum distance


int min_distance(int dist[], int visited[]) {
int min = INF, min_index = -1;

#pragma omp parallel for reduction(min:min_index)


for (int i = 0; i < V; i++) {
if (!visited[i] && dist[i] <= min) {
min = dist[i];
min_index = i;
}
}

return min_index;
}

// Dijkstra's algorithm using OpenMP


void dijkstra(int graph[V][V], int src) {
int dist[V]; // dist[i] stores the shortest distance from src to i
int visited[V] = {0}; // visited[i] will be 1 if vertex i is included in the shortest
path

// Initialize distances
for (int i = 0; i < V; i++) {
dist[i] = INF;
}
dist[src] = 0;

// Find the shortest path for all vertices


for (int count = 0; count < V - 1; count++) {
// Find the vertex with the minimum distance from the set of vertices not yet
processed
int u = min_distance(dist, visited);

// Mark the selected vertex as processed


visited[u] = 1;

// Update the distance values of the adjacent vertices of the selected vertex
#pragma omp parallel for
for (int v = 0; v < V; v++) {
if (!visited[v] && graph[u][v] != 0 && dist[u] != INF && dist[u] + graph[u][v]
< dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}

// Print the shortest distances


cout << "Vertex Distance from Source" << endl;
for (int i = 0; i < V; i++) {
cout << i << " \t\t " << dist[i] << endl;
}
}

int main() {
// Adjacency matrix representation of a graph
int graph[V][V] = {
{0, 9, 0, 0, 0, 14},
{9, 0, 10, 0, 0, 0},
{0, 10, 0, 11, 0, 0},
{0, 0, 11, 0, 6, 0},
{0, 0, 0, 6, 0, 9},
{14, 0, 0, 0, 9, 0}
};

int source = 0; // Starting node


auto start_parallel = high_resolution_clock::now();
dijkstra(graph, source);
auto end_parallel = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(end_parallel - start_parallel);
cout << "\nTime Taken: " << duration.count() << " milliseconds" << endl;

return 0;
}

OutPut

You might also like