-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.cpp
257 lines (221 loc) · 6.57 KB
/
Graph.cpp
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
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include "Graph.h"
using namespace std;
Graph::Graph(string f)
{
fileName = f;
result_cliques = {};
}
/* Busca estudante no vector de estudantes, se não existir, cria um. */
Student * Graph::getOrCreateStudent(int register_id, string name)
{
vector<Student*>::iterator it =
find_if(begin(students), end(students),
[=] (const Student* p) { return (p->getRegister() == register_id);});
if(it != students.end())
{
if((*it)->getName() == "" && name != "")
(*it)->setName(name);
return (*it);
}
else
{
Student *new_s = new Student(register_id, name);
students.push_back(new_s);
return new_s;
}
}
/* X ∩ N(v), coleta a interseção entre X e os vizinhos de V */
vector<Student *> Graph::XintersectNeighborsofV(vector<Student *> R, vector<Student *> P, vector<Student *> X, vector<Student *> v)
{
vector<Student *> Xtemp = {};
for(vector<Student *>::size_type i = 0; i < X.size(); i++)
{
for(vector<Student *>::size_type j = 0; j < v.back()->getAdjList().size(); j++)
{
if(X[i] == v.back()->getAdjList()[j])
Xtemp.push_back(X[i]);
}
}
return Xtemp;
}
/* P ∩ N(v), coleta a interseção entre P e os vizinhos de V */
vector<Student *> Graph::PintersectNeighborsofV(vector<Student *> R, vector<Student *> P, vector<Student *> X, vector<Student *> v)
{
vector<Student *> Ptemp = {};
for(vector<Student*>::size_type i = 0; i < P.size(); i++)
{
for(vector<Student *>::size_type j = 0; j < v.back()->getAdjList().size(); j++)
{
if(P[i] == v.back()->getAdjList()[j])
Ptemp.push_back(P[i]);
}
}
return Ptemp;
}
vector<Student *> Graph::XunionV(vector<Student *> R, vector<Student *> P, vector<Student *> X, vector<Student *> v)
{
vector<Student *> Xtemp;
Xtemp = X;
Xtemp.push_back(v.back());
return Xtemp;
}
/* P \ V, remove o vértice V do vetor P */
vector<Student *> Graph::PremoveV(vector<Student *> R, vector<Student *> P, vector<Student *> X, vector<Student *> v)
{
vector<Student *> Ptemp;
Ptemp = P;
for(vector<Student*>::size_type i = 0; i < Ptemp.size(); i++)
{
if(Ptemp[i] == v.back())
Ptemp.erase(Ptemp.begin() + i);
}
return Ptemp;
}
/* R U V, faz a união entre R e V */
vector<Student *> Graph::RunionV(vector<Student *> R, vector<Student *> P, vector<Student *> X, vector<Student *> v)
{
vector<Student *> Rtemp;
Rtemp = R;
Rtemp.push_back(v.back());
return Rtemp;
}
/*
* Implementa o algoritmo abaixo:
*
* WITHOUT PIVOTING:
*
* BronKerbosch1(R, P, X) :
* if P and X are both empty :
* report R as a maximal clique
* for each vertex v in P :
* BronKerbosch1(R U{ v }, P n N(v), X n N(v))
* P : = P \ {v}
* X: = X U {v}
*
* */
int Graph::BronKerbosch(vector<Student *> R, vector<Student *> P, vector<Student *> X)
{
if(P.empty() && X.empty())
{
result_cliques.push_back(R);
}
for(vector<Student *>::size_type i = 0; i < P.size(); i++)
{
vector<Student*> v_ = {};
v_.insert(v_.end(), P[i]);
BronKerbosch(RunionV(R, P, X, v_), PintersectNeighborsofV(R, P, X, v_), XintersectNeighborsofV(R, P, X, v_));
P = PremoveV(R, P, X, v_);
X = XunionV(R, P, X, v_);
if (P.empty())
return 1;
else
i = 0;
}
return 1;
}
/* Imprime o máximo e maximal do vetor de estudantes do grafo */
int Graph::printMaxes()
{
int max = 0;
for(vector <Student*>::size_type i = 0; i < result_cliques.size(); i++)
{
if(result_cliques[max].empty())
max = i;
else if(result_cliques[max].size() < result_cliques[i].size())
max = i;
}
cout << "MÁXIMO: " << endl;
for(vector <Student*>::size_type i = 0; i < result_cliques[max].size(); i++)
{
cout << result_cliques[max][i]->getName();
if(i+1 != result_cliques[max].size())
cout << ", ";
}
result_cliques.erase(result_cliques.begin() + max);
cout << "\n" << endl;
cout << "MAXIMAL: " << endl;
int maximal = 0;
for(vector <Student*>::size_type i = 0; i < result_cliques[maximal].size(); i++)
{
cout << result_cliques[maximal][i]->getName();
if(i+1 != result_cliques[maximal].size())
cout << ", ";
}
cout << "\n" << endl;
return 1;
}
/* Define a comparação entre estudantes para o sorter
* Compara o tamanho entre as listas de adjacência */
bool Graph::cmp(Student * a, Student * b)
{
return a->getAdjList().size() > b->getAdjList().size();
};
/* Organiza a lista de estudantes por tamanho da lista de
* adjacência de forma decrescente */
int Graph::studentsSorter()
{
sort(students.begin(), students.end(), cmp);
return 1;
}
/* Coleta o arquivo de entrada e trata ele para inserir
* no vetor de estudantes, formando o grafo */
int Graph::parseFile()
{
ifstream ifs;
ifs.open(fileName, std::ifstream::in);
string line;
vector<string> result;
char delim = '|';
if(ifs.is_open())
{
while(getline(ifs, line))
{
auto i = 0;
auto pos = line.find(delim);
while(pos != string::npos)
{
result.push_back(line.substr(i, pos-i));
i = ++pos;
pos = line.find(delim, pos);
if(pos == string::npos)
result.push_back(line.substr(i, line.length()));
}
/* Creates Students */
int register_id = atoi(result.at(1).c_str());
Student* cur_s = getOrCreateStudent(register_id, result.at(0));
/* Adds friends (if) to adj_list */
for(int i = 2; i < result.size(); ++i)
{
Student* cur = getOrCreateStudent(atoi(result.at(i).c_str()), "");
cur_s->addFriend(cur);
}
result.clear();
}
}
studentsSorter();
ifs.close();
return 1;
}
/* Getter para o vetor de estudantes */
vector<Student *>& Graph::getStudents()
{
return this->students;
}
int main()
{
Graph g("amigos_20172.txt");
g.parseFile();
vector<Student*> R, P, X;
for (Student* i : g.getStudents())
{
P.push_back(i);
cout << i->getRegister() << " : " << i->getAdjList().size() << endl;
}
g.BronKerbosch(R, P, X);
g.printMaxes();
return 0;
}