-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmst.cpp
More file actions
362 lines (296 loc) · 9.95 KB
/
Copy pathmst.cpp
File metadata and controls
362 lines (296 loc) · 9.95 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include <stdlib.h>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <iterator>
#include <algorithm>
#include <math.h>
#include <omp.h>
#define pair_t std::pair<int, int>
typedef struct munro
{
int rank;
double altitude;
std::string name;
std::string OSGridSquare;
int OSGridSquareEasting;
int OSGridSquareNorthing;
int easting;
int northing;
std::string latitude;
std::string longitude;
} munro_t;
std::map<int, munro_t> munroIDs;
// Data coordinates are of the form AB123123
// which is equivalent to AB1230012300 which
// gives a precision of 100m.
const int INPUT_COORDINATE_PRECISION = 100;
const int MAX = 80000;
int parent[MAX];
std::vector<std::pair<pair_t, pair_t> > GRAPH, MST; // ((weight, FORMS_CYCLE), (pu/rank, pv/rank))
// Current total network weighting.
int total = 0;
// Number of nodes (Munros).
const int N = 282;
// Number of edges in network.
// All Munros are connected -> 282 * 281.
const int E = 79242;
double distanceFromAToB(double x1, double y1, double x2, double y2);
void loadData(char* filename, std::vector<munro_t> &munros);
void computeWeights(std::vector<munro_t> &munros);
int findset(int x, int *parent);
void kruskal();
void reset();
void print(std::vector<munro_t> &munros);
int main(int argc, char** argv)
{
// Start timing.
double startTime = omp_get_wtime();
// Check that the user has provided the
// correct command-line parameters.
if (argc < 3)
{
std::cout << "USAGE: dijkstra FILENAME NTHREADS NNODES" << std::endl;
return 1;
}
// Get and set the number of threads to use.
int nthreads = atoi(argv[2]);
omp_set_num_threads(nthreads);
// Create a vector to store the input data.
std::vector<munro_t> munros;
// Read data into the vector and
// call reset.
std::cout << "Loading data..." << std::endl;
loadData(argv[1], munros);
reset();
// Compute the weights.
std::cout << "Computing weights..." << std::endl;
computeWeights(munros);
// Do Kruskal's algorithm
std::cout << "Finding MST..." << std::endl;
kruskal();
// Process results.
std::cout << "Writing output..." << std::endl;
print(munros);
// Stop timing
double endTime = omp_get_wtime();
std::cout << "nthreads = " << nthreads << ", time = " << endTime - startTime << std::endl;
return 0;
}
double distanceFromAToB(double x1, double y1, double x2, double y2)
{
double dx, dy = 0.0;
dx = x1 - x2;
dy = y1 - y2;
return sqrt(dx*dx + dy*dy);
}
void loadData(char* filename, std::vector<munro_t> &munros)
{
// Open the filename passed to the command-line.
std::ifstream inputFile(filename);
// Create a string to store the current line.
std::string currentLine;
// Parse the input file line by line.
while(std::getline(inputFile, currentLine))
{
std::stringstream stream(currentLine);
std::string field;
int easting, northing = 0;
// Create a new munro structure to hold
// the data from the current line.
munro_t currentMunro;
// Read in the parameters.
std::getline(stream, field, ',');
currentMunro.rank = atoi(field.c_str());
std::getline(stream, field, ',');
currentMunro.OSGridSquare = field;
if (field == "NC")
{
easting = 200000;
northing = 900000;
}
if (field == "NG")
{
easting = 100000;
northing = 800000;
}
if (field == "NH")
{
easting = 200000;
northing = 800000;
}
if (field == "NJ")
{
easting = 300000;
northing = 800000;
}
if (field == "NM")
{
easting = 100000;
northing = 700000;
}
if (field == "NN")
{
easting = 200000;
northing = 700000;
}
if (field == "NO")
{
easting = 300000;
northing = 700000;
}
std::getline(stream, field, ',');
currentMunro.OSGridSquareEasting = atoi(field.c_str());
currentMunro.easting = easting + INPUT_COORDINATE_PRECISION*atoi(field.c_str());
std::getline(stream, field, ',');
currentMunro.OSGridSquareNorthing = atoi(field.c_str());
currentMunro.northing = northing + INPUT_COORDINATE_PRECISION*atoi(field.c_str());
std::getline(stream, field, ',');
currentMunro.altitude = atoi(field.c_str());
std::getline(stream, field, ',');
currentMunro.name = field;
std::getline(stream, field, ',');
currentMunro.latitude = field;
std::getline(stream, field, ',');
currentMunro.longitude = field;
// Store for later.
munros.push_back(currentMunro);
// Munro ID = rank.
munroIDs[currentMunro.rank] = currentMunro;
}
}
void computeWeights(std::vector<munro_t> &munros)
{
munro_t* munrosArray = &munros[0];
// Loop 1 - over the munros
// This can be optimize by sharing the work between
// threads, as the order in which edges added to the
// GRAPH vector is not important.
double x1, y1, x2, y2, d = 0.0;
#pragma omp parallel for private(x1, y1, x2, y2, d) collapse(1)
for(int i=0; i < munros.size(); i++)
{
// The location of the current munro.
x1 = munrosArray[i].easting;
y1 = munrosArray[i].northing;
// Loop 2 - over the munros.
for(int j=0; j < munros.size(); j++)
{
// Get the location of the second munro.
x2 = munrosArray[j].easting;
y2 = munrosArray[j].northing;
// Don't measure distance between a munro
// and itself.
if ((x1 != x2) || (y1 != y2))
{
// Calculate the distance between the two.
d = distanceFromAToB(x1, y1, x2, y2);
#pragma omp critical
{
// Add to the graph.
GRAPH.push_back(std::pair<pair_t, pair_t>(pair_t(d, 0), pair_t(munrosArray[i].rank, munrosArray[j].rank)));
}
}
}
}
}
//////////////////////////////////////////////////////////////
// find the set (tree) which contains the node.
int findset(int x, int *parent)
{
// If a node is not its own parent...
if(x != parent[x])
// Recursviely find the node's parent
parent[x] = findset(parent[x], parent);
return parent[x];
}
void kruskal()
{
int i, pu, pv;
sort(GRAPH.begin(), GRAPH.end()); // increasing weight
// Loop 4 - over all edges
// Each edge has two nodes, u and v.
for(i=total=0; i<E; i++)
{
//Do we ALREADY know that this edge forms a cycle?
if(GRAPH[i].first.second == 0)
{
// Which set is node u in?
pu = findset(GRAPH[i].second.first, parent);
// Which set is node v in?
pv = findset(GRAPH[i].second.second, parent);
// If they belong to different sets then...
if(pu != pv)
{
// Add the edge's weighting to the total weighting
// of the MST.
total += GRAPH[i].first.first;
// Add the edge to the minimum spanning tree (MST)
MST.push_back(GRAPH[i]);
// Link the nodes
parent[pu] = parent[pv];
}
}
}
}
void reset()
{
// Loop 3 - order of access does not matter.
#pragma omp parallel
{
#pragma for nowait
for(int i=1; i<=N; i++)
{
parent[i] = i;
}
}
}
void print(std::vector<munro_t> &munros)
{
std::ofstream outputFile("mst.kml");
// Insert kml header including a folder for the lines.
outputFile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?><kml xmlns=\"http://earth.google.com/kml/2.2\"><Document><name>Munro MST</name>" << std::endl
<< "<Folder><name>lines</name><open>1</open>" << std::endl;
int i, sz;
sz = MST.size();
// Loop 5 - over all edges in the network;
for(i=0; i<sz; i++)
{
int ID1 = MST[i].second.first;
int ID2 = MST[i].second.second;
// Print to the console.
//std::cout << munroIDs[ID1].name << "(" << ID1 << ") --> "
// << munroIDs[ID2].name << "(" << ID2 << ") = "
// << MST[i].first.first/1000.0 << " Km" << std::endl;
// Write edges to kml file.
outputFile << "<Placemark><name>" << MST[i].first.first/1000.0 << " Km"
<< "</name><LineString><tessellate>1</tessellate><coordinates>" << std::endl
<< munroIDs[ID1].longitude << ","
<< munroIDs[ID1].latitude << ","
<< "0.000000" << std::endl
<< munroIDs[ID2].longitude << ","
<< munroIDs[ID2].latitude << ","
<< "0.000000" << std::endl
<< "</coordinates></LineString></Placemark>" << std::endl;
}
// Create a folder in the kml to store the points.
outputFile << "</Folder><Folder><name>points</name><open>1</open>" << std::endl;
// Loop 6 - over all the munros and add a point for each to the kml file.
for(i=1; i<N+1; i++)
{
outputFile << "<Placemark><name></name><description><![CDATA[<div dir=\"ltr\">"
<< munroIDs[i].rank << ". " << munroIDs[i].name
<< " (" << munroIDs[i].altitude << "m)" << std::endl
<< "</div>]]></description><Point><coordinates>" << std::endl
<< munroIDs[i].longitude << ","
<< munroIDs[i].latitude << ","
<< "0.000000</coordinates></Point></Placemark>" << std::endl;
}
// Insert kml footer
outputFile << "</Folder></Document></kml>" << std::endl;
std::cout << "Minimum cost = " << total/1000.0 << " Km" << std::endl;
}