-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
361 lines (300 loc) · 8.32 KB
/
Copy pathGraph.cpp
File metadata and controls
361 lines (300 loc) · 8.32 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
#include <vector>
#include <algorithm>
#include <iostream>
#include <random>
#include <stdexcept>
#include <fstream>
#include <regex>
#include <tuple>
#include <chrono>
#include <thread>
#include "Graph.hpp"
#include "Util.hpp"
#include <SFML/Graphics/Text.hpp>
#include "Line.hpp"
# define PI 3.14159265358979323846
struct lesser_first
{
inline bool operator() (const Edge& e1, const Edge& e2)
{
return (e1[0] < e2[0]);
}
};
Graph::Graph(std::vector<Node>& nodes, std::vector<Edge>& edges) : nodes(nodes), edges(edges), algorithm(None)
{
adjList = vector<list<int>>(nodes.size());
for (int i = 0; i < nodes.size(); ++i) {
this->nodes[i].id = i;
}
for (Edge& e : this->edges) {
if (e[1] < e[0]){
int tmp = e.nodes.x;
e.nodes.x = e.nodes.y;
e.nodes.y = tmp;
}
}
// Sort edges
sort(this->edges.begin(), this->edges.end(), lesser_first());
for (const Edge& e : this->edges) {
if (e[0] >= nodes.size() || e[1] >= nodes.size()) {
cout << "Invalid edge: " << e << endl;
exit(0xDEAD);
}
adjList[e[0]].push_back(e[1]);
adjList[e[1]].push_back(e[0]);
}
// Maximum node degree in the graph
maxDegree = 0;
for (int i = 0; i < adjList.size(); i++)
maxDegree = max(maxDegree, (int)adjList[i].size());
// Maximum edge weight in the graph
maxWeight = 0.f;
for (auto e: this->edges)
maxWeight = max(maxWeight, (float)e.weight);
}
void Graph::FruchtermanReingold(FruchtermanParams params) {
this->L = params.L;
this->cooling = params.cooling;
this->temp = DEFAULT_TEMP;
this->algorithm = Algorithm::FructhermanReingold;
this->width = params.W;
this->height = params.H;
this->done = false;
}
bool Graph::Update()
{
if (!done){
switch (algorithm) {
case Algorithm::FructhermanReingold:
iter++;
done = fructhermanReingoldStep();
break;
default:
throw std::invalid_argument("Algorithm not configured or not supported");
};
}
return !done;
}
void Graph::Reset() {
done = false;
temp = DEFAULT_TEMP;
}
bool Graph::fructhermanReingoldStep()
{
bool equilibrium = true;
vector<Vector2f> forces(nodes.size(), { 0, 0 });
// Iterate through each node pair and calculate repulsive forces
for (int i = 0; i < nodes.size(); ++i) {
for (int j = i + 1; j < nodes.size(); ++j) {
Vector2f rep = repulsive(nodes[i].pos, nodes[j].pos, L);
if (DEBUGGING)
cout << "Repulsive force " << i << "<->" << j << ": (" << rep.x << "," << rep.y << ")" << endl;
forces[i] += rep;
forces[j] -= rep;
}
}
// Iterate through all edges and calculate attractive forces
for (auto e : edges) {
int i = e.nodes.x;
int j = e.nodes.y;
Vector2f attr = attractive(nodes[i].pos, nodes[j].pos, L);
if (DEBUGGING)
cout << "Attractive force " << i << "<->" << j << ": (" << attr.x << "," << attr.y << ")" << endl;
forces[i] += attr;
forces[j] -= attr;
}
// Apply forces
for (int i = 0; i < nodes.size(); ++i) {
// Scale forces to number of nodes
forces[i] /= float(nodes.size());
// add attractive force, pulling node to the center the further its away
Vector2f center = { width / 2, height / 2 };
Vector2f centerPull = attractive(nodes[i].pos, center, CANVAS_HEIGHT) / float(nodes.size());
forces[i] += centerPull * Gravity;
int signX = (forces[i].x > 0.f) - (forces[i].x < 0.f);
int signY = (forces[i].y > 0.f) - (forces[i].y < 0.f);
// Use temperature to limit displacement
forces[i].x = min(abs(forces[i].x), temp) * signX;
forces[i].y = min(abs(forces[i].y), temp) * signY;
if ((abs(forces[i].x) > treshold) || (abs(forces[i].y) > treshold)) {
equilibrium = false;
}
nodes[i].pos += forces[i];
// Limit the node to be inside the window
nodes[i].pos.x = max(nodes[i].pos.x, float(nodes[i].shape.getRadius()));
nodes[i].pos.x = min(nodes[i].pos.x, float(width) - 2 * nodes[i].shape.getRadius());
nodes[i].pos.y = max(nodes[i].pos.y, float(nodes[i].shape.getRadius()));
nodes[i].pos.y = min(nodes[i].pos.y, float(height) - 2 * nodes[i].shape.getRadius());
nodes[i].shape.setPosition(nodes[i].pos);
}
temp *= cooling;
if (DEBUGGING)
cout << "Temp: " << temp << endl;
return equilibrium;
}
const float thickness = 1.5f;
void Graph::draw(tgui::CanvasSFML::Ptr &target, sf::Font font)
{
for (const Edge& e : edges) {
auto node1 = nodes[e[0]].shape;
auto node2 = nodes[e[1]].shape;
target->draw(sfLine(node1.getPosition() + node1.getOrigin(), node2.getPosition() + node2.getOrigin(), thickness));
}
for (int i = 0; i < nodes.size(); ++i) {
// scale node size based on degree
float scaledR = nodeMin + (nodeMax - nodeMin) * (adjList[i].size() / (float)maxDegree);
nodes[i].shape.setOrigin(scaledR / 2, scaledR / 2);
nodes[i].shape.setRadius(scaledR);
target->draw(nodes[i].shape);
if (showLabels) {
sf::Text label{ nodes[i].label, font};
label.setFillColor(sf::Color::Red);
label.setCharacterSize(18);
FloatRect numRect = label.getGlobalBounds();
Vector2f numRectCenter(numRect.width / 2.0f + numRect.left, numRect.height / 2.0f + numRect.top);
label.setOrigin(numRectCenter);
label.setPosition(nodes[i].pos.x, nodes[i].pos.y);
target->draw(label);
}
}
}
void Graph::setNodeDimensions(float nodeMin, float nodeMax)
{
this->nodeMin = nodeMin;
this->nodeMax = nodeMax;
}
void Graph::setShowLabels(bool showLabels)
{
this->showLabels = showLabels;
}
void Graph::RandomLayout(Vector2f pos, float L) {
for (Node& n : nodes) {
n.pos = { pos.x + generateRandomNumber(-L, L), pos.y + generateRandomNumber(-L, L) };
n.shape.setPosition(n.pos);
}
};
void Graph::RandomCircularLayout(Vector2f pos, float R) {
for (Node& n : nodes) {
float random = generateRandomNumber(0, 1);
float angle = random * 2 * PI;
n.pos = { pos.x + R * cos(angle), pos.y + R * sin(angle) };
n.shape.setPosition(n.pos);
}
};
void Graph::add_node(Node n)
{
nodes.push_back(n);
adjList.resize(adjList.size() + 1);
}
void Graph::add_edge(Edge e)
{
edges.push_back(e);
adjList[e[0]].push_back(e[1]);
adjList[e[1]].push_back(e[0]);
}
const std::vector<Node>& Graph::Nodes() const
{
return nodes;
}
const vector<Edge>& Graph::Edges() const
{
return edges;
}
std::ostream& operator<<(std::ostream& os, const Graph& obj) {
os << "Graph" << endl << "nodes: ";
print_vector(obj.nodes);
cout << "Edges:";
print_vector(obj.edges);
return os;
}
Graph Graph::fromGML(string file)
{
ifstream src(file);
string str;
vector<Node> nodes;
vector<Edge> edges;
while (getline(src, str)) {
if (str.find("graph") != string::npos) {
break;
}
}
int minus = 0;
bool first = true;
bool hasWeights = true;
while (getline(src, str)) {
auto trimmed = trim_copy(str);
if (trimmed.find("node") != string::npos) {
// Parse node
while (str.find("id") == string::npos)
getline(src, str);
// 'id <id>'
trim(str);
int id = stoi(str.substr(3)) - minus;
if (first) {
if (id == 0) {
minus = 0;
}else {
minus = 1;
id -= 1;
}
first = false;
}
do
getline(src, str);
while (trim_copy(str) == "");
string label = to_string(id);
if (str.find("label") != string::npos) {
// parse 'label <l>'
trim(str);
// trim the quotes
if (str[str.length() - 1] == '"') {
str.pop_back();
label = str.substr(7);
}
else label = str.substr(6);
}
nodes.emplace_back(Node::from_id(id, label));
// parse rest of node
while (str.find(']') == string::npos && getline(src, str));
}
else if(trimmed.find("edge") != string::npos) {
// Parse edge
getline(src, str); // skip '['
// 'source <id>'
while (str.find("source") == string::npos)
getline(src, str);
trim(str);
// 'target <id>'
int source_id = stoi(str.substr(7)) - minus;
while (str.find("target") == string::npos)
getline(src, str);
trim(str);
int target_id = stoi(str.substr(7)) - minus;
// 'value <num>'
float w = 1;
if (hasWeights) {
while (str.find("value") == string::npos) {
getline(src, str);
if (str.find("]") != string::npos) {
hasWeights = false;
break;
}
}
if (hasWeights) {
trim(str);
w = stof(str.substr(6));
}
}
edges.emplace_back(Edge(source_id, target_id, w));
// parse rest of edge
while (str.find(']') == string::npos && getline(src, str));
}
}
if (DEBUGGING) {
cout << nodes.size() << " Nodes:" << endl;
print_vector(nodes);
cout << edges.size() << " Edges:" << endl;
print_vector(edges);
}
return Graph(nodes, edges);
}