Professional Documents
Culture Documents
TEAM MEMBERS
Rohith Krishna -18BCE0537
Sai Srujith D -18BCE0560
SUBMITTED TO
Dr. PREETHA EVANGELINE D
School of Computer Science and Engineering,
VIT UNIVERSITY
MONTH OF SUBMISSION
OCTOBER-2020
S.NO TABLE OF CONTENTS
1 ABSTRACT
2 INTRODUCTION
3 LITERATURE SURVEY
4 PROPOSED WORK
5 RESULT
7 REFERENCES
1.ABSTRACT
The project deals with implementation of Dijkstra algorithm i.e. All Pair Shortest
Path. This algorithm is implemented using parallel programming concept for
faster solution. Dijkstra algorithm has overcome the drawbacks of Floyd Warshall
algorithm and Bellman Ford Algorithm.
For parallel programming, this project is implemented using MPI for which C
programming is used. The purpose of developing this project is to find the
shortest path between all the present nodes in a graph. This project can be
implemented for Airline Systems, Transportation services, Courier Services,
Networking etc.
From the results it is observed that parallel algorithm is considerably effective for
large graph sizes and MPI implementation is better in terms of performance over
serial implementation of Dijkstra’s algorithm.
The primary goal of this project is to implement the Dijkstra algorithm in Parallel
with the help of MPI Programming to find the shortest path between two nodes
and we compare both serials implemented Dijkstra algorithm and parallel
implemented algorithm.
2. INTRODUCTION
Finding the shortest path between two objects or all objects in a graph is a
common task in solving many day to day and scientific problems. The algorithms
for finding shortest path find their application in many fields such as social
networks, bioinformatics, aviation, routing protocols, Google maps etc. Shortest
path algorithms can be classified into two types: single source shortest paths and
all pair shortest paths. There are many different algorithms for finding the all pair
shortest paths. Some of them are Dijkstra algorithm and Johnson’s algorithm.
All pair shortest path algorithm which is also known as Dijkstra algorithm was
developed in 1962. This algorithm follows the methodology of the dynamic
programming. The algorithm is used for graph analysis and finds the shortest
paths (lengths) between all pair of vertices or nodes in a graph. The graph is a
weighted directed graph with negative or positive edges.
The algorithm is limited to only returning the shortest path lengths and does not
return the actual shortest paths with names of nodes.
3.LITERATURE SURVEY
In the graph partitioning approach, a given graph is divided into multiple pieces
of the same size in such a way that there are few connections between the pieces
(Hendrickson and Leland 1995; Möhring et al. 2005).
For example, to perform graph partitioning, Möhring et al. (2005) used different
data structures, such as a grid, k-dimensional (k-d) trees, and quadtrees to perform
graph partitioning.
Thus, one has to rely on certain heuristics that cannot be guaranteed to work in
every situation. Romeijn and Smith (1999) developed a hierarchical network
decomposition algorithm to partition a network into multiple pieces, which could
be independently solved to yield approximate shortest paths for a large-scale
network.
Finding minimum spanning tree of graph studied the two sequential algorithms.
Graph calculates the single source shortest path and all pair shortest path using
two serial algorithms, it can be converted into parallel which resulting the number
of iterations reduce consisting less time. Based on two sequential algorithms
Floyd and Dijkstra’s find the serial implementation. Then using OpenMP and
MPI convert the serial code into parallel as less amount of time based on the
speedup factor and efficiency and cost. [Parallelization of Shortest Path
Algorithm Using OpenMP and MPI Rajashri Awari Dept.of Info.Tech. MTech.
Yeshwantrao Chavan College of Engineering Nagpur, India]
Dijkstra algorithm:
Dijkstra algorithm is All Pair Shortest Path finder. It is mainly used to overcome
the drawbacks of Floyd’s and Bellman Ford Algorithm. It considers negative
weight present in the graph. In Floyd Warshall algorithm every node of the graph
is visited and the shortest path is computed. The Dijkstra algorithm is an example
of dynamic programming.
Existing Model:
Sequential algorithm
for k = 0 to N-1
For i = 0 to N-1
for j = 0 to N-1
Endfor
The space complexity of the algorithm is Θ(N2 ). This algorithm requires the
adjacency matrix as the input.
In this project we have implemented the parallel version of all pair shortest path
algorithm in MPI.
From the results we found that parallel version gave speedup benefits over
sequential one, but these benefits are more observable for large datasets.
for j = 0 to N-1
Endfor
Endfor
Endfor
The Dijkstra algorithm The Dijkstra algorithm compares all possible paths
through the graph between each pair of vertices. Consider a graph G (V, E) where
V is no. of vertices and E is no. of edges. For computing minimum path between
each pair of node Wk. is computed where k ranges from 1 to V. For computing
shortest from i to j, W [i, j] = W [i, k] +W [k, j] INPUT: A graph G (V, E) where
V is a set of vertices and E is set of weighted edges between these vertices. A
source vertex form V.
OUTPUT: The distance of shortest paths between the source vertex and every
vertex in G.
Function: Read_n
Purpose: Read in the number of rows in the matrix on process 0 and broadcast
Function: Build_blk_col_type
Input arguments: n: number of rows in the matrix and the block column
Function: Read_matrix
Purpose: Read in an nxn matrix of ints on process 0, and distribute it among
the processes so that each process gets a block column with n rows and n/p
columns
Input arguments: n: the number of rows in the matrix and the submatrices
loc_n = n/p: the number of columns in the submatrices
Function: Print_local_matrix
Function: Print_matrix
Purpose: Print the matrix that's been distributed among the processes.
submatrix
Function: Dijkstra
Function: Find_min_dist
Purpose: Find the vertex u with minimum distance to 0 (dist[u]) among the
vertices whose distance to 0 is not known.
Function: Global_vertex
vertex
Input arguments: n: the number of vertices dist: distances from 0 to each vertex
v: dist[v]
Function: Print_paths
[1 0 3 3 4 2 1 3],
[2 3 0 6 5 7 2 1],
[2 1 1 0 4 3 2 7],
[2 2 3 4 0 6 1 2],
[1 1 3 2 5 0 2 3],
[3 4 5 2 1 2 0 2].
[2 3 1 6 3 2 1 0] ]
From the above execution time and from the fact that performance is
inversely proportional to time of execution , the performance of parallel
execution of djikstra’s algorithm is 10^4 times more efficient than
serial execution
6.CODE AND OUTPUT SCREENSHOTS
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mpi.h>
void Dijkstra(int mat[], int dist[], int pred[], int n, int loc_n, int my_rank,
MPI_Comm comm);
MPI_Comm comm);
void Print_dists(int loc_dist[], int n, int loc_n, int my_rank, MPI_Comm comm);
void Print_paths(int loc_pred[], int n, int loc_n, int my_rank, MPI_Comm comm);
int main(int argc, char* argv[]) {
MPI_Comm comm;
MPI_Datatype blk_col_mpi_t;
MPI_Init(&argc, &argv);
comm = MPI_COMM_WORLD;
MPI_Comm_size(comm, &p);
MPI_Comm_rank(comm, &my_rank);
n = Read_n(my_rank, comm);
loc_n = n/p;
loc_mat = malloc(n*loc_n*sizeof(int));
loc_dist = malloc(n*loc_n*sizeof(int));
loc_pred = malloc(n*loc_n*sizeof(int));
free(loc_mat);
free(loc_dist);
free(loc_pred);
MPI_Type_free(&blk_col_mpi_t);
printf("time of execution=%f\n",end-start);
MPI_Finalize();
return 0;
int n;
if (my_rank == 0)
scanf("%d", &n);
return n;
}
MPI_Datatype Build_blk_col_type(int n, int loc_n) {
MPI_Datatype block_mpi_t;
MPI_Datatype first_bc_mpi_t;
MPI_Datatype blk_col_mpi_t;
&blk_col_mpi_t);
MPI_Type_commit(&blk_col_mpi_t);
MPI_Type_free(&block_mpi_t);
MPI_Type_free(&first_bc_mpi_t);
return blk_col_mpi_t;
if (my_rank == 0) {
mat = malloc(n*n*sizeof(int));
MPI_Scatter(mat, 1, blk_col_mpi_t,
if (my_rank == 0) free(mat);
char temp[MAX_STRING];
int i, j;
cp = temp + strlen(temp);
if (loc_mat[i*loc_n + j] == INFINITY)
else
cp = temp + strlen(temp);
}
sprintf(cp, "\n");
cp = temp + strlen(temp);
printf("%s\n", temp);
if (my_rank == 0) {
if (mat[i*n + j] == INFINITY)
printf(" i ");
else
printf("\n");
free(mat);
}
void Initialize_matrix(int mat[], int loc_dist[], int loc_pred[], int known[],
loc_pred[v] = 0;
known[v] = 0;
if (my_rank == 0) {
known[0] = 1;
void Dijkstra(int mat[], int loc_dist[], int loc_pred[], int n, int loc_n,
known = malloc(loc_n*sizeof(int));
int g_min_dist;
if (loc_u < INFINITY) {
my_min[0] = loc_dist[loc_u];
} else {
my_min[0] = INFINITY;
my_min[1] = INFINITY;
u = glbl_min[1];
g_min_dist = glbl_min[0];
if (u/loc_n == my_rank) {
loc_u = u % loc_n;
known[loc_u] = 1;
if (!known[loc_v]) {
loc_dist[loc_v] = new_dist;
loc_pred[loc_v] = u;
}
}
free(known);
MPI_Comm comm) {
loc_u = INFINITY;
if (!loc_known[loc_v])
loc_u = loc_v;
loc_min_dist = loc_dist[loc_v];
return loc_u;
void Print_dists(int loc_dist[], int n, int loc_n, int my_rank, MPI_Comm comm)
int v;
if (my_rank == 0) {
dist = malloc(n*sizeof(int));
if (my_rank == 0) {
printf("---- ---------\n");
printf("\n");
free(dist);
}
void Print_paths(int loc_pred[], int n, int loc_n, int my_rank, MPI_Comm comm) {
if (my_rank == 0) {
pred = malloc(n*sizeof(int));
if (my_rank == 0) {
path = malloc(n*sizeof(int));
printf("---- ---------\n");
count = 0;
w = v;
while (w != 0) {
path[count] = w;
count++;
w = pred[w];
}
printf("0 ");
printf("\n");
free(path);
free(pred);
}
OUTPUT SCREENSHOT
Parallel algorithm being executed for a graph with 8 vertices and their
respective unit weights and the time of execution is also shown at the end
7.REFERENCES
[1]Kairanbay Magzhan, Hajar Mat Jani”A review and evaluation of shortest path
algorithms”.
[3] http://www.nvidia.com/object/what-is-gpu-computing.html
[4] Efficient multi GPU algorithm for All pair shortest path. [8] G. Venkataraman, S.
Sahni, and S. Mukhopadhyaya, A blocked all-pairs shortest-paths algorithm, J. Exp.
Algorithmics.
[7] [Parallelization of Shortest Path Algorithm Using OpenMP and MPI Rajashri Awari
Dept.of Info.Tech. MTech. Yeshwantrao Chavan College of Engineering Nagpur,
India]
[8] Approximation Algorithm for Shortest Path in Large Social Networks Dennis Nii
Ayeh Mensah * , Hui Gao and Liang Wei Yang