-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelationshipFinder.cpp
More file actions
284 lines (256 loc) · 9.56 KB
/
RelationshipFinder.cpp
File metadata and controls
284 lines (256 loc) · 9.56 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
/*
This is a class that parses a gedcom file into a structure of
person objects. Created by Jacob McCoy on 28 November 2023
*/
#include "RelationshipFinder.h"
#include <climits>
#include <sstream>
#include <iostream>
#include <chrono>
// chunk size determines how many iterations of a for loop each thread does
// at a time. change as desired
#define CHUNK_SIZE 25
// leave defined if you want OpenMP in your code
#define OMP_TEST_MODE
// the constructor creates the adjacency matrix dynamically
RelationshipFinder::RelationshipFinder(std::unordered_map<fs_id, Person> person_map)
{
// copy in the person map
this->family_map = person_map;
// make the dynamic arrays
this->matrix_width = person_map.size();
this->adjacency_matrix = new unsigned int[this->matrix_width * this->matrix_width];
this->prev = new unsigned int[this->matrix_width * this->matrix_width];
// get the max value
this->max_dist = UINT_MAX - this->matrix_width - 1;
// fill it with default values
auto start_time = std::chrono::high_resolution_clock::now();
#ifdef OMP_TEST_MODE
#pragma omp parallel for schedule(guided, CHUNK_SIZE)
#endif
for (int i = 0; i < this->matrix_width; i++)
{
for (int j = 0; j < this->matrix_width; j++)
{
// by default, the distance from one node to another is infinity
this->adjacency_matrix[i*matrix_width + j] = this->max_dist;
// by default, to get from i from j, you go directly to i
this->prev[i*matrix_width + j] = i;
}
// // put zeros on the diagonal for the adjacency matrix
// // and itself on the diagonal for the previous matrix
this->adjacency_matrix[i*matrix_width + i] = 0;
this->prev[i*matrix_width + i] = i;
}
// print out some timing stats
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
#ifdef OMP_TEST_MODE
std::cout << "OpenMP ";
#endif
std::cout << "Matrix Filling Time: " << duration.count() << " us" << std::endl;
// for each item in the map
for (auto& person : person_map)
{
// pull out the id
id current_id = person.second.GetID();
// on that id's row, add a 1 in the column of each relationship
// spouse relationships
for (int i = 0; i < person.second.GetSpouses().size(); i++)
{
this->adjacency_matrix[current_id * this->matrix_width + person.second.GetSpouses().at(i)] = 1;
}
// child relationships
for (int i = 0; i < person.second.GetChildren().size(); i++)
{
this->adjacency_matrix[current_id * this->matrix_width + person.second.GetChildren().at(i)] = 1;
}
// father relationships
for (int i = 0; i < person.second.GetFathers().size(); i++)
{
this->adjacency_matrix[current_id * this->matrix_width + person.second.GetFathers().at(i)] = 1;
}
// mother relationships
for (int i = 0; i < person.second.GetMothers().size(); i++)
{
this->adjacency_matrix[current_id * this->matrix_width + person.second.GetMothers().at(i)] = 1;
}
}
// // error checking. matrix should be symmetric
for (int i = 0; i < this->matrix_width; i++)
{
for (int j = 0; j< this->matrix_width; j++)
{
if (this->adjacency_matrix[i*this->matrix_width + j] != this->adjacency_matrix[j*this->matrix_width + i])
{
std::cerr << "Error: (" << i << "," << j << ") != (" << j << "," << i << ").";
std::cerr << "(" << i << "," << j << ")=" << this->adjacency_matrix[i*this->matrix_width + j] << " but ";
std::cerr << "(" << j << "," << i << ")=" << this->adjacency_matrix[j*this->matrix_width + i] <<"\n";
// look for the errors
for (auto it = this->family_map.begin(); it != this->family_map.end(); ++it)
{
if (it->second.GetID() == i)
{
std::cout << i << ": " <<it->second.GetName() << "fs: " << it->first << std::endl;
}
if (it->second.GetID() == j)
{
std::cout << j << ": " <<it->second.GetName() << "fs: " <<it->first << std::endl;
}
}
}
}
}
}
RelationshipFinder::~RelationshipFinder()
{
// deallocate the dynamic arrays
delete[] this->adjacency_matrix;
delete[] this->prev;
}
std::string RelationshipFinder::ToString() const
{
// format the string as a grid, with x meaning there's no path
std::stringstream to_return;
for (int i = 0; i < this->matrix_width; i++)
{
for (int j = 0; j < this->matrix_width; j++)
{ unsigned int to_insert = this->adjacency_matrix[i*this->matrix_width + j];
if (to_insert == this->max_dist)
{
to_return << "x" << " ";
}
else
{
to_return << to_insert << " ";
}
}
to_return << std::endl;
}
return to_return.str();
}
std::string RelationshipFinder::ToStringPath() const
{
// same as ToString(), just uses the path matrix not the distance matrix
std::stringstream to_return;
for (int i = 0; i < this->matrix_width; i++)
{
for (int j = 0; j < this->matrix_width; j++)
{ unsigned int to_insert = this->prev[i*this->matrix_width + j];
if (to_insert == this->max_dist)
{
to_return << "x" << " ";
}
else
{
to_return << to_insert << " ";
}
}
to_return << std::endl;
}
return to_return.str();
}
void RelationshipFinder::FloydRelationshipFinder()
{
unsigned int matrix_width = this->matrix_width;
// start the timer
auto start_time = std::chrono::high_resolution_clock::now();
// go through every node
for (int k = 0; k < matrix_width; k++)
{
#ifdef OMP_TEST_MODE
#pragma omp parallel for schedule(guided, CHUNK_SIZE)
#endif
// for every pair of nodes
for (int i = 0; i < matrix_width; i++)
{
for (int j = 0; j < matrix_width; j++)
{
// if the path from i to j is longer than the path from i to j that cuts through k
if (this->adjacency_matrix[i*matrix_width + j] >
this->adjacency_matrix[i*matrix_width + k] +
this->adjacency_matrix[k*matrix_width + j])
{
// make the path that cuts through k be the new path
this->adjacency_matrix[i*matrix_width + j] =
this->adjacency_matrix[i*matrix_width + k] +
this->adjacency_matrix[k*matrix_width + j];
// get the path
this->prev[i*matrix_width + j] = this->prev[k*matrix_width + j];
}
}
}
}
// print out the timing of everything
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
#ifdef OMP_TEST_MODE
std::cout << "OpenMP ";
#endif
std::cout << "Floyd's Algorithm Time: " << duration.count() << " us" << std::endl;
return;
}
void RelationshipFinder::DisplayPath(fs_id start, fs_id end)
{
// check for valid IDs
bool start_valid = false;
bool end_valid = false;
//Probably can parallelize. can't really have a data race I don't think
for (auto it = this->family_map.begin(); it != this->family_map.end(); ++it)
{
// if it isn't true and they're equal...
if((!start_valid) && it->first == start)
{
start_valid = true;
}
// do for end point as well
if((!end_valid) && it->first == end)
{
end_valid = true;
}
}
// if one isn't valid, end it all
if (!(start_valid && end_valid))
{
std::cout << "Invalid ID Found" << std::endl;
return;
}
// pull out the indices to use on the matrix
id start_index, current_index, end_index;
current_index = start_index = this->family_map[start].GetID();
end_index = this->family_map[end].GetID();
// base case: no connection between the two
if (this->prev[start_index * this->matrix_width + end_index] == this->max_dist)
{
std::cout << "No relationship" << std::endl;
return;
}
// otherwise, there is a relationship: print it out
std::cout << "Relationship: " << std::endl;
for (auto it = this->family_map.begin(); it != this->family_map.end(); ++it)
{
if (it->second.GetID() == current_index)
{
std::cout << it->second.GetName() << std::endl;
break;
}
}
// go until you hit the end index
while (current_index != end_index)
{
// calculate the new index
current_index = prev[end_index * this->matrix_width + current_index];
Person person;
// go find the right person
for (auto it = this->family_map.begin(); it != this->family_map.end(); ++it)
{
if (it->second.GetID() == current_index)
{
// print out their name if it's the right one
std::cout << it->second.GetName() << std::endl;
break;
}
}
}
return;
}