-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
201 lines (169 loc) · 4.39 KB
/
graph.cpp
File metadata and controls
201 lines (169 loc) · 4.39 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
#include "graph.h"
map<int, vector<int>> vertex;
int V_source, V_destiny, interactions;
vector<vector<int>> cliquesmax;
int readGraph(string nameFile)
{
fstream FileReader;
FileReader.open(nameFile.c_str());
if (FileReader.is_open())
{
FileReader >> V_source >> V_destiny >> interactions;
for (int i = 0; i < interactions; i++)
{
int source, destiny;
FileReader >> source >> destiny;
auto iterator = find(vertex[source].begin(), vertex[source].end(), destiny);
if (source == destiny)
{
vertex[source].push_back(destiny);
}
else if (iterator == vertex[source].end())
{
vertex[source].push_back(destiny);
vertex[destiny].push_back(source);
}
}
}
else
{
cout << "Erro na Leitura do Arquivo" << endl;
FileReader.close();
return -1;
}
FileReader.close();
return V_source;
}
void printDegreeGraph()
{
cout << "Vertices e seus Graus: " << endl;
for (int i = 1; i <= vertex.size(); i++)
{
cout << "Vertice: " << i;
cout << " Grau: " << vertex[i].size() << endl;
}
cout << endl;
}
double coeficienteAglomeracao(int vertexIndex)
{
double coefficient;
double triangle = 0;
vector<int> v = vertex[vertexIndex];
int sizeNeighbors = v.size();
for (int i = 0; i < sizeNeighbors; i++)
{
int first = v[i];
for (int j = 0; j < sizeNeighbors; j++)
{
int second = v[j];
for (int k = 0; k < vertex[first].size(); k++)
{
if (second == vertex[first][k])
{
triangle += 0.5;
}
}
}
}
if (sizeNeighbors > 1)
{
coefficient = (2 * triangle) / (sizeNeighbors * (sizeNeighbors - 1));
}
else
{
coefficient = 0.0;
}
return coefficient;
}
double averageCoefficient()
{
double coefficient = 0.0;
for (int i = 1; i <= V_source; i++)
{
coefficient += coeficienteAglomeracao(i);
}
return (coefficient / V_source);
}
void printCoefficient()
{
cout << "Vertices e seus Coeficientes de Aglomeracao: " << endl;
for (int i = 1; i <= V_source; i++)
{
cout << "Vertice -> " << i << " Coeficiente de Aglomeracao -> " << coeficienteAglomeracao(i) << endl;
}
cout << endl;
cout << "Media dos Coeficientes: " << averageCoefficient() << endl;
}
void bronKerbosch(vector<int> r, vector<int> p, vector<int> x)
{
//clique maximal
if (p.empty() && x.empty())
{
cliquesmax.push_back(r);
}
//backtracking
if (p.empty() && !(x.empty()))
{
return;
}
// -------------------------------------------------------------------------
// Map auxiliar da lista do P e do X
map<int, bool> pmem, xmem;
for (int i = 0; i <= vertex.size(); i++)
{
pmem[i] = false;
xmem[i] = false;
}
for (int i = 0; i < p.size(); i++)
{
pmem[p[i]] = true;
}
for (int i = 0; i < x.size(); i++)
{
xmem[x[i]] = true;
}
// -------------------------------------------------------------------------
for (int i = 0; i < p.size(); i++)
{
int pivot = p[i];
r.push_back(pivot);
vector<int> pprox, xprox;
for (int neighborIndex = 0; neighborIndex < vertex[pivot].size(); neighborIndex++)
{
if (pmem[vertex[pivot][neighborIndex]])
{
pprox.push_back(vertex[pivot][neighborIndex]);
}
if (xmem[vertex[pivot][neighborIndex]])
{
xprox.push_back(vertex[pivot][neighborIndex]);
}
}
bronKerbosch(r, pprox, xprox);
r.pop_back();
pmem[pivot] = false;
xmem[pivot] = true;
}
}
void printMaximalCliques()
{
vector<int> r;
vector<int> p;
vector<int> x;
for (int i = 1; i <= vertex.size(); i++)
{
p.push_back(i);
}
bronKerbosch(r, p, x);
cout << "Lista dos cliques maximais: " << endl;
for (int i = 0; i < cliquesmax.size(); i++)
{
cout << i + 1 << " - ";
for (int j = 0; j < cliquesmax[i].size(); j++)
{
cout << cliquesmax[i][j] << " ";
}
cout << endl;
}
cout << endl;
}