-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphalgorithms.cpp
More file actions
507 lines (409 loc) · 15.5 KB
/
graphalgorithms.cpp
File metadata and controls
507 lines (409 loc) · 15.5 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#include <queue>
#include <stack>
#include <vector>
#include "measuretool.h"
#include "graphsalgorithms.h"
#include "heap.h"
#include "disjoint.h"
using namespace algorithms;
using namespace std;
using namespace graphs;
using namespace std::chrono;
AFirstSearch::AFirstSearch()
{
}
/**
* @brief AFirstSearch::BFS This function has the algorith to buil a tree T, from a graph G, using the BFS algorithm.
* Also, it check if the Graph acomplishes with the Bipartiteness property.
* @param root root Node which the algorithm start to build the resulting tree.
* @param G The Original graph.
* @param T The resulting tree.
* @param steps The log output.
* @param checkBipartitenes A flag which indicates if it performes the Bipartitenes validation (True) or not (false).
*/
void AFirstSearch::BFS(long root, Graph &T, stringstream &steps, bool checkBipartitenes)
{
measure::MeasureTool tool;
queue<long> levelQueue;
vector<bool> discovey(T.countNodes(),false);
vector<int> parents(T.countNodes(), -1);
vector<int> layerByNode(T.countNodes(), -1);
vector<bool> isOdd(T.countNodes(), false);
steps << "Tree BFS:" << endl;
if(T.empty())
{
steps << "\tError: It's not possible to build a Tree from an empty Graphic." << endl;
return;
}
T.disableAll();
high_resolution_clock::time_point tBegin = high_resolution_clock::now();
T[root].enable(true);
levelQueue.push(root);
discovey[root] = true;
parents[root] = -1;
layerByNode[root] = 0; //The root node is layer 0.
isOdd[root] = true; //The firt node is Odd
steps << "\tStart to build Tree with BFS, from node [" << root << "]" << endl;
while(!levelQueue.empty()) //T(n)
{
long node = levelQueue.front();
vector<std::pair<long,long>> adjNodes = T[node].getAdyacentNodes();
levelQueue.pop();
//Check childs
for(std::pair<long,long> p : adjNodes )
{
long idNode_ = p.first;
long idEdge_ = p.second;
// T(n-1), where n -1 represents all the possible nodes adyacentes.
//But it is important to considered that each node wll be procecesd only if it has not been disovered
if(!discovey[idNode_])
{
steps << "\t\tNew Node [" << idNode_ << "] discovery from Node [" << node << "] with Edge (" << idEdge_ << ")" << endl;
T[idNode_].enable(true);
T(idEdge_).enable(true);
parents[idNode_] = node;
levelQueue.push(idNode_); //Add the node into the queue (Adding to one level upper)..
discovey[idNode_] = true;
layerByNode[idNode_] = layerByNode[node] + 1; //The new child discovered has a upper level.
isOdd[idNode_] = !isOdd[node]; //The child must be the opposit of its parent (Odd or Even).
}
else
{
if(!checkBipartitenes && isOdd[idNode_] == isOdd[node])
steps << "\t\t**The edge with nodes [" << idNode_ << "] and [" << node << "] are not allowing the Graph to comply with the Bipartiteness propertie.**" << endl;
}
}
}
tool.measureTime(tBegin,steps);
}
/**
* @brief AFirstSearch::DFS Is the main function which prepare al the parameters to invoke the recursive function DFS
* @param root Node which the algorithm start to build the resulting tree.
* @param G The Original graph.
* @param T The resulting tree.
* @param steps The log output.
*/
void AFirstSearch::DFS(long root, Graph &T, stringstream &steps)
{
measure::MeasureTool tool;
steps << "Tree DFS:" << endl;
if(T.empty())
{
steps << "\tError: It's not possible to build a Tree from an empty Graphic." << endl;
return;
}
T.disableAll();
steps << "\tStart to build Tree with DFS, from node [" << root << "]" << endl;
high_resolution_clock::time_point tBegin = high_resolution_clock::now();
vector<short> explored(T.countNodes(),0);
vector<int> parents(T.countNodes(),-1);
DFS(root, explored, parents, T, steps); //recursive function to replace a stack structure, use the call stack
tool.measureTime(tBegin,steps);
}
/**
* @brief AFirstSearch::DFS this is the recursive function wich implement a Stak (Call Sctack) that is used to implement the
* DFS algorithm instead a Stack structure.
* @param root Node which the algorithm continue building the resulting tree.
* @param explored Array of nodes which has been explored and haven't.
* @param parents Array of nodes which are parents into the tree.
* @param G The Original graph.
* @param T The resulting tree.
* @param steps The log output.
*/
void AFirstSearch::DFS(long root, vector<short>& explored, vector<int>& parents,
Graph &T, stringstream &steps )
{
T[root].enable(true);
explored[root] = 1; //explored
steps << "\t\tNew Node [" << root << "] explored from Parent Node [" << parents[root] << "]" << endl;
Node rv = T[root];
vector<pair<long,long>>adj = rv.getAdyacentNodes();
for(pair<long,long> vAd : adj)
{
int idNode_ = vAd.first;
int idEdge_ = vAd.second;
if(!explored[idNode_])
{
parents[idNode_] = root;
DFS(idNode_, explored, parents, T, steps);
T(idEdge_).enable(true);
}
}
}
/**
* @brief AFirstSearch::checkDAG
*
* + The activeNode vector has Ids of each node which is active into the DGA's algorithm tree.
* + The array incommingCount has the total node that are incomming to the node, each time that a node is removed it drecrease
* the incommins count from its adjancents nodes.
*
* @param G
* @param steps
*/
bool AFirstSearch::checkDAG(Graph &G, stringstream &steps)
{
measure::MeasureTool tool;
steps << "Checking Directed Acyclic Graph (DGA) propertie:" << endl;
high_resolution_clock::time_point tBegin = high_resolution_clock::now();
queue<long> topologic;
bool result = checkDAG(G,topologic,steps);
if(result)
{
steps << "\tThis graph meets the DAG property and its topologyc founded is:" << endl
<< "\t\t[ ";
while(!topologic.empty())
{
steps << topologic.front() << " ";
topologic.pop();
}
steps << "]" << endl;
}
else
{
vector<long> vrtxs = G.NodeKeys();
steps << "\tThis graph doesn´t meet the DAG property because has been found a cicle betwen nodes:" << endl
<< "\t\t[ ";
for(long k : vrtxs)
steps << k << " ";
steps << "]" << endl;
steps << "\tThis was the topologic that could be detected before found the cycle:" << endl
<< "\t\t[ ";
while(!topologic.empty())
{
steps << topologic.front() << " ";
topologic.pop();
}
steps << "]" << endl;
}
tool.measureTime(tBegin,steps);
return result;
}
bool AFirstSearch::checkDAG(graphs::Graph &G, queue<long>& topologic , std::stringstream &steps)
{
vector<long> vrtxs = G.NodeKeys();
//1.- find a Vertex without incoming nodes.
for(size_t i=0; i < vrtxs.size(); i++)
{
if(G[vrtxs[i]].countNodesAdy(true) == 0)
{
topologic.push(vrtxs[i]);
G.removeNode(vrtxs[i]);
if(G.countNodes() > 0)
return checkDAG(G,topologic,steps);
else
return true;
}
}
return false;
}
/**
* @brief dijkstra
* @param from The start node.
* @param to The end node
* @param G The Graph
* @param steps Stringstream wich received the log.
* @return A stack of tuples with: <NodeId, EdgeId, Cost>
*/
stack<tuple<long,long,long >> shortpath::dijkstra(long from, long to, graphs::Graph& G, std::stringstream& steps)
{
measure::MeasureTool tool;
steps << "Dijkstra: Searching for the shorted Path from [" << from << "] to [" << to << "]" << endl;
collections::Heap<Node*> heap; //To handle the universe of Nodes which are not discovere and to update the cost of each node
vector<long> fromNode(G.countNodes(),-1); //To handle the cost to reach each node.
vector<long> fromEdge(G.countNodes(),-1); //To handle the cost to reach each node.
vector<size_t> costNode(G.countNodes()); //To store the cost of each node.
stack<tuple<long,long,long >> path; //<idNode, idEdge, Cost>
high_resolution_clock::time_point tBegin = high_resolution_clock::now();
//1. Init Structures //T(n log(n))
vector<long> nodeKeys = G.NodeKeys();
for(long keyNode : nodeKeys)
{
stringstream temp;
bool isRoot = keyNode == from;
costNode[keyNode] = isRoot ? 0 : collections::HEAP_INIFITY_KEY;
Node* n = &G[keyNode];
if(keyNode != from)
heap.Insert(collections::HEAP_INIFITY_KEY, n,temp); //Store a copy of the Node with its key.
}
//2.Process each node.
long currentNode = from;
while(!heap.empty()) //T(n)
{
stringstream temp;
vector<pair<long,long>> adjs = G[currentNode].getAdyacentNodes();
//2.1. Update Costs
for(pair<long,long> x : adjs)
{
long node = x.first;
size_t newCost = G(x.second).Length();
newCost += costNode[currentNode];
if(costNode[node] > newCost)
{
Node* n = &G[node];
size_t position = heap.Position(n);
heap.changeKey(position, newCost,temp);
costNode[node] = newCost;
fromNode[node] = currentNode;
fromEdge[node] = x.second;
}
}
bool success;
Node* n = heap.ExtractFirst(success,temp);
currentNode = n->ID();
}
currentNode = to;
while(currentNode != -1)
{
path.push(tuple<long,long,long>(currentNode, fromEdge[currentNode],costNode[currentNode]));
currentNode = fromNode[currentNode];
}
steps << endl;
tool.measureTime(tBegin,steps);
return path;
}
/**
* @brief minimun_spanning::prim This function implements the PRIM Algorith to find the Minimum Spannig Tree.
* @param G The Graph which will be optimized.
* @param steps
*/
void minimun_spanning::prim(long start, graphs::Graph& G, std::stringstream& steps)
{
//bool result{true};
measure::MeasureTool tool;
steps << "PRIM: Minimum Spanning Tree." << endl;
vector<long> fromEdge(G.countNodes(),-1); //To handle the cost to reach each node.
vector<size_t> costNode(G.countNodes()); //To store the cost of each node.
collections::Heap<Node*> heap; //To handle the universe of Nodes which are not discovere and to update the cost of each node
G.disableAll(); // T(n + m); n=nodes, m= edges
high_resolution_clock::time_point tBegin = high_resolution_clock::now();
//T(n log(n) )
vector<long> nodeKeys = G.NodeKeys();
for(long keyNode : nodeKeys) //T(n)
{
stringstream temp;
bool isRoot = keyNode == start;
costNode[keyNode] = isRoot ? 0 : collections::HEAP_INIFITY_KEY;
Node* n = &G[keyNode];
if(keyNode != start)
heap.Insert(collections::HEAP_INIFITY_KEY, n,temp); // T(log(n))
}
long currentNode = start;
while(!heap.empty()) // T(n)
{
stringstream temp;
vector<pair<long,long>> adjs = G[currentNode].getAdyacentNodes();
G[currentNode].enable(true); //Enable (Discover) the node
steps << "\t Node: " << currentNode << " has been discovered" << endl;
//2.1. Update Costs
for(pair<long,long> x : adjs) // T(m long(n-1))
{
long node = x.first;
size_t newCost = G(x.second).Length();
newCost += costNode[currentNode];
if(costNode[node] > newCost)
{
Node* n = &G[node];
size_t position = heap.Position(n);
heap.changeKey(position, newCost,temp); // T( long(n-1) )
costNode[node] = newCost;
//fromNode[node] = currentNode;
//Update Edges which has the lesser cost to the adj node.
if(fromEdge[node] != -1)
{
G(fromEdge[node]).enable(false);
steps << "\t Edge: " << fromEdge[node] << " has been disabled." << endl;
}
fromEdge[node] = x.second;
G(fromEdge[node]).enable(true);
steps << "\t Edge: " << fromEdge[node] << " has been enabled." << endl;
}
}
bool success;
Node* n = heap.ExtractFirst(success,temp); //T(1)
currentNode = n->ID();
}
G[currentNode].enable(true); //Enable (Discover) the node
steps << "\t Node: " << currentNode << " has been discovered" << endl;
steps << endl;
tool.measureTime(tBegin,steps);
}
/**
* @brief minimun_spanning::kruskal
* @param start
* @param G
* @param steps
*/
void minimun_spanning::kruskal(graphs::Graph& G, std::stringstream& steps)
{
std::stringstream temp;
measure::MeasureTool tool;
steps << "Kruskal: Minimum Spanning Tree." << endl;
collections::Disjoint_Set<long> djSet;
collections::Heap<Edge*> heap; //To handle the universe of Nodes which are not discovere and to update the cost of each node
G.disableAll(); // T(n + m); n=nodes, m= edges
high_resolution_clock::time_point tBegin = high_resolution_clock::now();
djSet.MakeUnionFind(G.NodeKeys()); //T(n)
vector<long> edges = G.EdgeKeys();
for(long m : edges)
{
Edge* edge = &G(m);
heap.Insert(G(m).Length(),edge,temp );
}
while(!heap.empty())
{
bool success{true};
Edge* m = heap.ExtractFirst(success,temp);
long s = djSet.Union( djSet.Find(m->From()), djSet.Find(m->To()) );
if(s >= 0)
{
steps << "\tNew Set Created/Update[" << s << "]" << endl
<< "\t\tNodes [" << m->From() << "-" << m->To() << "] linked to the with edge [" << m->ID() << "]" << endl;
G[m->From()].enable(true);
G[m->To()].enable(true);
G(m->ID()).enable(true);
}
}
tool.measureTime(tBegin,steps);
}
/**
* @brief minimun_spanning::findClusters
* @param G
* @param steps
*/
void minimun_spanning::findClusters(long size, graphs::Graph& G, std::stringstream& steps)
{
std::stringstream temp;
measure::MeasureTool tool;
steps << "Finding [" << size << "] Clusters." << endl;
collections::Disjoint_Set<long> djSet;
collections::Heap<Edge*> heap; //To handle the universe of Nodes which are not discovere and to update the cost of each node
G.disableAll(); // T(n + m); n=nodes, m= edges
high_resolution_clock::time_point tBegin = high_resolution_clock::now();
djSet.MakeUnionFind(G.NodeKeys()); //T(n)
vector<long> edges = G.EdgeKeys();
for(long m : edges)
{
Edge* edge = &G(m);
heap.Insert(G(m).Length(),edge,temp );
}
size--;
while(!heap.empty())
{
bool success{true};
Edge* m = heap.ExtractFirst(success,temp);
if(heap.size() >= size)
{
long s = djSet.Union( djSet.Find(m->From()), djSet.Find(m->To()) );
if(s >= 0)
{
steps << "\tNew Set Created/Update[" << s << "]" << endl
<< "\t\tNodes [" << m->From() << "-" << m->To() << "] linked to the with edge [" << m->ID() << "]" << endl;
G(m->ID()).enable(true);
}
//counter++;
}
G[m->From()].enable(true);
G[m->To()].enable(true);
}
tool.measureTime(tBegin,steps);
}