forked from QasimAbbas28/QClique-Euro-PAR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph-new.h
366 lines (314 loc) · 10.1 KB
/
graph-new.h
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
/*
* This file is part of QClique.
*
* QClique is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QClique is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QClique. If not, see <https://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <string>
#include <algorithm>
#include <thread>
#include <cmath>
#include "vertex-edge.h"
#include "clique.h"
using namespace std;
class UnDirectedGraph {
vector<Edge> edges;
map<string, Vertex> vertices;
vector<Clique> cliques;
Clique maxCl;
public:
Vertex addVertex(int label, float weight){
Vertex v(label, weight);
vertices[label]=v;
return v;
}
map<string, Vertex> getVertices(){return vertices;}
vector<Edge> getEdges(){return edges;}
Edge addEdge(int from, int to){
if (vertices.find(from) != vertices.end() && vertices.find(to) != vertices.end()){
Vertex vfrom = vertices.find(from)->second;
Vertex to = vertices.find(to)->second;
Edge e(vfrom,vto);
(*vfrom).addEdge(e);
(*vto).addEdge(e);
edges.push_back(e);
return e;
}
else{
//needt o handle case where vertices did not exist.
return 0;
}
}
Edge getEdge(int from, int to){
if (vertices.find(from) != vertices.end() && vertices.find(to) != vertices.end()){
Vertex v1 = vertices.find(from)->second;
Vertex v2 = vertices.find(to)->second;
Edge e = (v1).getEdgeTo(to);
return e;
}
else {
//need to handle case where vertices did not exist.
return 0;
}
}
void removeEdge(int from, int to){
Edge * e = getEdge(from,to);
if (e != 0){
edges.erase(remove(edges.begin(),edges.end(),e),edges.end());
(*e).getV1()->removeEdge(e);
(*e).getV2()->removeEdge(e);
}
//handle case where edge did not exist?
}
Vertex getVertexWithLabel(int l){
if (vertices.find(l) != vertices.end())
return vertices.find(l)->second;
else
return 0;
}
void removeVertex(int l){
Vertex v = getVertexWithLabel(l);
if (v != 0){
vector<Edge> edges = getVertexWithLabel(l)->getEdges();
for (vector<Edge>::iterator it = edges.begin(); it != edges.end(); ++it){
string from = (it)->getV1()->getLabel();
string to = (it)->getV2()->getLabel();
removeEdge(from,to);
}
vertices.erase(l);
}
else {
//Need to handle case where vertex does not exist.
}
}
vector<Vertex> whereCanIGo(Vertex v)
{
vector<Vertex> destinations;
vector<Edge> edges = v->getEdges();
for (vector<Edge>::const_iterator it = edges.begin(); it != edges.end(); ++it) {
if ((it)->getV1() != v){
destinations.push_back((it)->getV1());
}
if ((it)->getV2() !=v) {
destinations.push_back((it)->getV2());
}
}
destinations.push_back(v);
return destinations;
}
void do_partition_find_maximum_clique() {
int verticesSize = vertices.size();
int threadscont = 1;
std::thread myThreads[threadscont];
float chunkSize = ceil(verticesSize / threadscont);
for(int i=1; i<=threadscont; i++) {
map<string, Vertex*> newVertices = getSubMap((i-1)*chunkSize, i*chunkSize);
myThreads[i-1] = std::thread(&UnDirectedGraph::findCliques, this, newVertices);
}
for (int i=0; i<threadscont; i++){
myThreads[i].join();
}
cout<<"All cliques found by all threads.."<<endl;
int maxWeight = 0;
Clique maxClique;
for (Clique c: cliques) {
int currentWeight = c.getTotalWeight();
if(currentWeight > maxWeight ) {
maxClique = c;
}
}
cout << "Maximum CLique weight is: " << maxClique.getTotalWeight() << endl;
cout << "Max Clique is :" << endl;
for (string i: maxClique.getVertices()) {
cout << i << ", ";
}
cout << endl;
}
void findCliques(map<int, Vertex> gTemp) {
bool found = false;
cout << "Finding Cliques..." << endl;
float max = 0;
auto iter = gTemp.begin();
int count = 0;
while (iter != gTemp.end()) {
Vertex v = iter->second;
string label = iter->first;
vector<Edge> edges = v->getEdges();
for(Edge e : edges) {
Clique c = Clique();
c.addVertex(label);
c.updateWeight(v->getWeight());
Vertex v1 = e->getV1();
if(v1->getLabel()!= label) {
c.addVertex(v1->getLabel());
c.updateWeight(v1->getWeight());
cliques.push_back(c);
vector<Edge> neighbours = v1->getEdges();
for(Edge n : neighbours) {
Vertex g1 = n->getV1();
Vertex g2 = n->getV2();
if(g1->getLabel()!=v1->getLabel() && g1->getLabel()!=label) {
if(g1->getEdgeTo(label)!=0) {
// Vertex * temp1 = this->getVertexWithLabel(g1->getLabel());
c.addVertex(g1->getLabel());
c.updateWeight(g1->getWeight());
cliques.push_back(c);
}
}
if(g2->getLabel()!=v1->getLabel() && g2->getLabel()!=v->getLabel()) {
if(g2->getEdgeTo(label)!=0) {
// Vertex * temp1 = this->getVertexWithLabel(g2->getLabel());
c.addVertex(g2->getLabel());
c.updateWeight(g2->getWeight());
cliques.push_back(c);
}
}
}
} else {
Vertex v2 = e->getV2();
if(v2->getLabel()!= label) {
c.addVertex(v2->getLabel());
c.updateWeight(v2->getWeight());
cliques.push_back(c);
vector<Edge> neighbours = v->getEdges();
for(Edge n : neighbours) {
Vertex g1 = n->getV1();
Vertex g2 = n->getV2();
if(g1->getLabel()!=v2->getLabel() && g1->getLabel()!=label) {
if(g1->getEdgeTo(label)!=0) {
Vertex temp1 = this->getVertexWithLabel(g1->getLabel());
c.addVertex(temp1->getLabel());
c.updateWeight(temp1->getWeight());
cliques.push_back(c);
}
}
if(g2->getLabel()!=v2->getLabel() && g2->getLabel()!=label) {
if(g2->getEdgeTo(label)!=0) {
Vertex temp1 = this->getVertexWithLabel(g2->getLabel());
c.addVertex(temp1->getLabel());
c.updateWeight(temp1->getWeight());
cliques.push_back(c);
}
}
}
}
}
// cliques.push_back(c);
}
++count;
++iter;
// free(v);
}
// auto iter2 = gTemp.begin();
// // int count = 0;
// while (iter2 != gTemp.end()) {
// Vertex * vt = iter2->second;
// string label = iter2->first;
// vector<Edge *> edges = vt->getEdges();
// Clique c1 = Clique();
// c1.addVertex(vt->getLabel());
// c1.updateWeight(vt->getWeight());
// for(Edge * e1 : edges) {
// Vertex * vt1 = e1->getV1();
// if(vt1->getLabel()!= vt->getLabel()) {
// if(vt1->getEdgeTo(vt->getLabel())!=0) {
// Vertex * temp1 = this->getVertexWithLabel(vt1->getLabel());
// c1.addVertex(temp1->getLabel());
// c1.updateWeight(temp1->getWeight());
// cliques.push_back(c1);
// }
// } else {
// Vertex * vt2 = e1->getV2();
// if(vt2->getLabel()!= vt->getLabel()) {
// if(vt2->getEdgeTo(vt->getLabel())!=0) {
// Vertex * temp1 = this->getVertexWithLabel(vt2->getLabel());
// c1.addVertex(temp1->getLabel());
// c1.updateWeight(temp1->getWeight());
// cliques.push_back(c1);
// }
// }
// }
// }
// // ++count;
// ++iter2;
// }
}
map<string, Vertex> getSubMap(int start, int end) {
map<string, Vertex> newVertices;
int verticesSize = vertices.size();
if(end> verticesSize) {
end = verticesSize;
}
auto iter = vertices.begin();
int count = 0;
while (iter != vertices.end()) {
if(count>=start && count<=end) {
Vertex v = iter->second;
newVertices[iter->first]=v;
}
++count;
++iter;
}
return newVertices;
}
};
void printGraph(UnDirectedGraph * t){
map<string,Vertex> vertices = t->getVertices();
for (map<string, Vertex>::iterator it = vertices.begin(); it != vertices.end(); ++it){
cout << it->first <<": ";
vector<Edge> edges = it->second->getEdges();
for (vector<Edge>::iterator jit = edges.begin(); jit != edges.end(); ++jit){
string l1 = (jit)->getV1()->getLabel();
string l2=(jit)->getV2()->getLabel();
if (l1 != it->first){cout << l1 << ", ";}
if (l2 != it->first){cout << l2 << ", ";}
}
cout << endl;
}
}
bool isPath(UnDirectedGraph t, string from, string to)
{
Vertex vfrom = t->getVertexWithLabel(from);
Vertex vto = t->getVertexWithLabel(to);
if (vfrom == 0 || vto == 0) {
return false;
}
if (from==to) {
return true;
}
UnDirectedGraph g = t;
map<string, Vertex> vertices = t->getVertices();
vector<Edge> edges = t->getEdges();
vector<Vertex> verticesToCheck;
verticesToCheck.push_back(vfrom);
vertices.erase(from);
while (verticesToCheck.size() != 0){
vector<Vertex> destinations = t->whereCanIGo(verticesToCheck[0]);
verticesToCheck.erase(verticesToCheck.begin());
for (vector<Vertex>::const_iterator it = destinations.begin(); it != destinations.end(); ++it) {
//
if (vertices.find((it)->getLabel()) != vertices.end()) {
if ((it)->getLabel()==to) {
return true;
}
verticesToCheck.push_back((it));
vertices.erase((it)->getLabel());
}
}
}
return false;
}