-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpar_dij.cpp
More file actions
201 lines (156 loc) · 3.72 KB
/
par_dij.cpp
File metadata and controls
201 lines (156 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <stdio.h>
#include <omp.h>
#include<algorithm>
#define INFINITY 100000
int V,E;
//Structure for vertex
typedef struct
{
int label;
bool visited;
} Vertex;
//Structure for directed edge from u to v
typedef struct
{
int u;
int v;
} Edge;
//Printing Shortest Path Length
void printShortestPathLength(int *path_length)
{
printf("\nVERTEX \tSHORTEST PATH LENGTH \n");
int i;
for(i = 0; i < V; i++)
{
printf("%d \t",i);
if (path_length[i]<INFINITY)
printf("%d\n",path_length[i]);
else
printf("Infinity\n");
}
}
//Finds weight of the edge that connects Vertex u with Vertex v
int findEdgeWeight(Vertex u, Vertex v, Edge *edges, int *weights)
{
int i;
for(i = 0; i < E; i++)
{
if(edges[i].u == u.label && edges[i].v == v.label)
{
return weights[i];
}
}
// If no edge exists, weight is infinity
return INFINITY;
}
//Get the minimum path length among the paths
int minimimPathLength(int *path_length, Vertex *vertices)
{
int i;
int min_path_length = INFINITY;
for(i = 0; i < V; i++)
{
if(vertices[i].visited == true)
{
continue;
}
else if(vertices[i].visited == false && path_length[i] < min_path_length)
{
min_path_length = path_length[i];
}
}
return min_path_length;
}
int minimimPathVertex(Vertex *vertices, int *path_length)
{
int i;
int min_path_length = minimimPathLength(path_length, vertices);
//Get the vertex with the minimum path length
//Mark it as visited
for(i = 0; i < V; i++)
{
if(vertices[i].visited == false && path_length[vertices[i].label] == min_path_length)
{
vertices[i].visited = true;
return i;
}
}
}
// Dijkstra Algorithm
void Dijkstra_Parallel(Vertex *vertices, Edge *edges, int *weights, Vertex *root)
{
double parallel_start, parallel_end;
int path_length[V];
// Mark first vertex as visited, shortest path = 0
root->visited = true;
path_length[root->label] = 0;
int i, j;
// Compute distance to other vertices
for(i = 0; i < V;i++)
{
if(vertices[i].label != root->label)
{
path_length[vertices[i].label] = findEdgeWeight(*root, vertices[i], edges, weights);
}
else
{
vertices[i].visited = true;
}
}
parallel_start = omp_get_wtime();
// External For Loop
for(j = 0; j < V; j++)
{
Vertex u;
// Obtain the vertex which has shortest distance and mark it as visited
int h = minimimPathVertex(vertices, path_length);
u = vertices[h];
//Update shortest path wrt new source
//Internal For Loop, Parallelising the computation
#pragma omp parallel for schedule(static) private(i)
for(i = 0; i < V; i++)
{
if(vertices[i].visited == false)
{
int c = findEdgeWeight( u, vertices[i], edges, weights);
path_length[vertices[i].label] = std::min(path_length[vertices[i].label], path_length[u.label] + c);
}
}
}
parallel_end = omp_get_wtime();
printShortestPathLength(path_length);
printf("\nRunning time: %lf ms\n", (parallel_end - parallel_start)*1000);
}
int main()
{
printf("==========PARALLEL IMPLEMENTATION OF DIJKSTRA ALGORITHM==========\n");
printf("\n\t\t\t\t\t\tS. DHANYA ABHIRAMI\n\t\t\t\t\t\t16BCE0965\n");
printf("Enter number of vertices: ");
scanf("%d",&V);
printf("Enter number of edges: ");
scanf("%d",&E);
Vertex vertices[V];
Edge edges[E];
int weights[E];
int i;
for(i = 0; i < V; i++)
{
Vertex a = { .label =i , .visited=false};
vertices[i] = a;
}
printf("\nEnter these details \nFROM \tTO \tWEIGHT\n");
int from,to,weight;
for(i = 0; i < E; i++)
{
scanf("%d %d %d",&from,&to,&weight);
Edge e = {.u = from , .v = to};
edges[i] = e;
weights[i] = weight;
}
int source;
printf("\nEnter Source Vertex: ");
scanf("%d",&source);
Vertex root = {source, false};
Dijkstra_Parallel(vertices, edges, weights, &root);
return 0;
}