-
Notifications
You must be signed in to change notification settings - Fork 13
/
Dijkstra-OpenMP.c
281 lines (164 loc) · 3.82 KB
/
Dijkstra-OpenMP.c
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Author: Jose F. Martinez Rivera
// Course: ICOM4036 - 040
// Professor: Wilson Rivera Gallego
// Assignment 2 - OpenMP Implementation
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <omp.h>
#define INT_MAX 100000
#define TRUE 1
#define FALSE 0
#define V 8
#define E 11
//boolean type
typedef int bool;
//Represents an edge or path between Vertices
typedef struct
{
int u;
int v;
} Edge;
//Represents a Vertex
typedef struct
{
int title;
bool visited;
} Vertex;
//Prints the array
void printArray(int *array)
{
int i;
for(i = 0; i < V; i++)
{
printf("Path to Vertex %d is %d\n", i, array[i]);
}
}
//OpenMP Implementation of Dijkstra's Algorithm
void DijkstraOMP(Vertex *vertices, Edge *edges, int *weights, Vertex *root)
{
double start, end;
root->visited = TRUE;
int len[V];
len[(int)root->title] = 0;
int i, j;
for(i = 0; i < V;i++)
{
if(vertices[i].title != root->title)
{
len[(int)vertices[i].title] = findEdge(*root, vertices[i], edges, weights);
}
else{
vertices[i].visited = TRUE;
}
}
start = omp_get_wtime();
for(j = 0; j < V; j++){
Vertex u;
int h = minPath(vertices, len);
u = vertices[h];
//OpenMP Parallelization Starts here!!!
#pragma omp parallel for schedule(runtime) private(i)
for(i = 0; i < V; i++)
{
if(vertices[i].visited == FALSE)
{
int c = findEdge( u, vertices[i], edges, weights);
len[vertices[i].title] = minimum(len[vertices[i].title], len[u.title] + c);
}
}
}
end = omp_get_wtime();
printArray(len);
printf("Running time: %f ms\n", (end - start)*1000);
}
//Finds the edge that connects Vertex u with Vertex v
int findEdge(Vertex u, Vertex v, Edge *edges, int *weights)
{
int i;
for(i = 0; i < E; i++)
{
if(edges[i].u == u.title && edges[i].v == v.title)
{
return weights[i];
}
}
return INT_MAX;
}
//Returns the minimum between two integers
int minimum(int A, int B)
{
if( A > B)
{
return B;
}
else{
return A;
}
}
//Visits the vertices and looks for the lowest weight from the vertex
int minWeight(int *len, Vertex *vertices)
{
int i;
int minimum = INT_MAX;
for(i = 0; i < V; i++)
{
if(vertices[i].visited == TRUE)
{
continue;
}
else if(vertices[i].visited == FALSE && len[i] < minimum)
{
minimum = len[i];
}
}
return minimum;
}
//Localizes the vertex with the lowest weight path
int minPath(Vertex *vertices, int *len)
{
int i;
int min = minWeight(len, vertices);
for(i = 0; i < V; i++)
{
if(vertices[i].visited == FALSE && len[vertices[i].title] == min)
{
vertices[i].visited = TRUE;
return i;
}
}
}
int main(void)
{
Vertex vertices[V];
// Edge edges[E];
// int weights[E];
//----------------------------------Graph Base Test-------------------------------------//
Edge edges[E] ={{0, 4}, {0, 6}, {0,2}, {4,6}, {4,7}, {0, 7}, {7, 3}, {3, 1}, {2,5}, {2, 1}, {5,3}};
int weights[E] = {10, 90, 30, 20, 20, 50, 10, 20, 10, 10, 10};
int i = 0;
for(i = 0; i < V; i++)
{
Vertex a = { .title =i , .visited=FALSE};
vertices[i] = a;
}
//----------------------------------Graph Base Test-------------------------------------//
//--------------------------------Graph Randomizer-----------------------------------//
// srand(time(NULL));
// int i = 0;
// for(i = 0; i < V; i++)
// {
// Vertex a = { .title =(int) i, .visited=FALSE};
// vertices[i] = a;
// }
// for(i = 0; i < E; i++)
// {
// Edge e = {.u = (int) rand()%V , .v = rand()%V};
// edges[i] = e;
// weights[i] = rand()%100;
// }
//--------------------------------Graph Randomizer-----------------------------------//
Vertex root = {0, FALSE};
printf("OpenMP Results for Small Graph of 8 Vertices:\n");
DijkstraOMP(vertices, edges, weights, &root);
}